setting dynamic x coordinate of second lable in xcode objective c - iphone

i have a problem that i have two lables,
1) will contain a long text
2) this will be just More button (link)
so i want that when the first text ends then the second label starts right from where it end. but the problem is that the text is variable so i could not find any way to make it dynamic.
example
text of first row
"this is text of first label" "more"
text of second line
"test best" "more"
in the example the rows are of table view and two lables are separated by " so i want second label starting point from where the text in first lable ends
looking forward for some solution
Thanks & Regards!

NSString has some methods to calculate its size when displaying using given font (e.g. sizeWithFont: method) - you can use it to determine text width and place your more button accordingly (someZZZZ parameters must be available on runtime):
CGFloat firstLabelWidth = [firstLabel.text sizeWithFont:firstLabel.font].width;
CGFloat moreX = firstLabel.frame.origin.x + firstLabelWidth + someGap;
moreButton.frame = CGRectMake(moreX, moreY, someWidth, someHeight);
You may need to add some validation for cases when text in first label is too long to fit the screen etc, but in general this code should work.

Related

Display what AVSpeechSynthesizer is showing as subtitles with Swift

I have an array with quotations, and am using a button to start a random quotation playing from the array.
I'm wondering, how would I have the selected random quotation also displayed in the ViewController so that people can read what the speechsynthesizer is reading?
How would I have the selected random quotation also displayed in the ViewController so that people can read what the speechsynthesizer is reading?
The best way to highlight the vocalized word is using the speechSynthesizer:willSpeakRangeOfSpeechString:utterance: method of the AVSpeechSynthesizerDelegate protocol.
Instead of copying and pasting the code snippet here, take a look at this complete and useful example (ObjC and Swift) that displays the vocalized word in a bold font with the speech synthesis.
...is there some way of using Subtitles.sizeToFit() so that the Label is split into 3 or more rows, instead of just "one row till infinity"?
In the Xcode Interface Builder - Attributes Inspector, set the value of the label property Lines to 0 and adapt your constraints to get a multiline presentation.
Add a label to your view and and set the quote text to it
quoteLabel.text = randomQuote
To show your label on multiple rows add:
label.numberOfLines = 0

Get current height of xwpf row, cell, paragraph

I am using XWPF to read .docx file which contain a table. In this table I get row then cell then I add a paragraph inside and in this paragraph I add multiple Run (with different style of font)
My question is (I know I don’t have posibility to get when word add new page or it is out of scope to me...?) But what I want achieve is get current height in my loop of the tableRow or tableCell is this possible? I mean something like this squelette code
XWPFTableRow r = ...;
For(...){
r.getCell(0).addNewParagraph(0).newRun().setText(...);
// at this step of programm I want height of each cell and each paragraph,
// is this possible? Because each iteration my table row height increment..
}
I tried tableRow.getHeight() to get row height but it get always 0, someone have idea to get row or cell or paragraph height?

Center Multi-Line Text on UIButton using IB

How do you center text line-by-line in a UIButton using Interface Builder? I am scouring the options and just don't see it. Here's the button:
You can't set the text to be centered in your nib. But you can change the alignment in your code:
- (void)viewDidLoad {
[super viewDidLoad];
self.myButton.titleLabel.textAlignment = UITextAlignmentCenter;
}
I know this is an old question, but I came across it in my own attempt to center the multi-line text of a UIButton in IB. What I found is that by default, when "title" is set to "plain" and "line break" is set to "word wrap" the longest line of the title text is centered and the other lines are left justified to this line (similar to the OP's screen capture).
In order to have all the lines centered properly, "title" needs to be changed to "attributed." This provides many more options to customize the appearance of the title text. Center each of the lines of text (you can now actually change the alignment for each line individually). Also be sure to set "line breaking" to "word wrap" under "more..." above the text. There seems to be a bug with how this line breaking option behaves, at least in Xcode 4.5 at this time, because the text on the button in IB will look incorrect, truncating everything except the first line. It seems the "word wrap" and truncate options are interpreted backwards in IB, but if you run the app it behaves correctly in the simulator.
Actually you can do it in interface builder.
Just set Title to "Attributed" and then choose center alignment.
#from comments : To wrap you need to set Line Break to Character Wrap or Word Wrap.
P.S : This might not render in xcode. But, it will work at runtime.
You can set the center multiline text in UIButton through storyboard.
This is how you make the text have two or more lines.
Set the below key Path at
Identity Inspector --> User defined runtime attributes --> add new key value pair with below
titleLabel.textAlignment - NSNumber - 1
and
titleLabel.numberOfLines - NSNumber - 5 - or use "0" meaning "any number"
It will look like this:
Note that (2016) unfortunately it does not actually show the two or more lines of text in Storyboard (you see only the first one), but it works perfectly when you run in simulator or device.
For IB set Title to "Attributed" and select center alignment (like Alexander Danilov suggested)
But if you want to do it in code using Swift 4:
// center button text
yourButton.titleLabel?.textAlignment = .center
// enable multiline if needed
yourButton.titleLabel?.numberOfLines = 0
Not all options are done using Interface Builder therefore you must do some of them by coding, and usually we do them inside the function viewDidLoad.
To center your text inside a button by code you can use the following:
button1.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
You can use the same technique to align the text to any direction, for example to the left:
button1.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
But this will take the text too much to the left and you might want to have some space before it keeping the alignment to the left, so you add an inset after the aligning code as follows:
button1.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
In this case we are pushing the text from the Y axis by 10 points. I say here points not pixels because as you know Apple uses the points technique to measure distances to be able to adapt easily between normal display and retina display (where retina is 2 times the normal one).
I haven't tried it out yet, but I think a way to do it might be create a CGRect on top of your button, then use it as a frame, create a label, and then you can play with the label, set the textAlignment property to be UITextAlignmentCenter, and set the background color to be clear.
This works with uitableview but I don't know whether that will work for button. Hope this helps.

UILabel Fit Text Perfectly

I have a UILabel that needs to be set to multiple lines of text. I have set the noOfLines property to 0 and I have the following code:
self.aboutLabel.lineBreakMode = UILineBreakModeWordWrap;
self.aboutLabel.text = self.selectedVegetableCategory.about;
[self.aboutLabel sizeToFit];
The above code does make the text span multiple lines but still some of the text is missing. I am using CoreData to retrieve the text from db and the fields are varchar but they do not have any limit on it (I have not placed any limit on the number of characters). Does, varchar by default has a limit of number of characters?
UPDATE 1: I just checked again and this is definitely the UILabel issue since CoreData returns the complete text without truncation.
UPDATE 2: The original problem still exists and now there is a new problem. I put a new label just below the original label and now the new label does not move down when the original label expands.
What is the frame of the label before & after the call to sizeToFit?
I think that you'll have to provide it a specific frame if you want it to word wrap properly.

How to wrap the data in a label of a cell in iphone?

im passing huge data to a label of a cell.So i need to wrap the line.I tried the below methods ------- cell.textlabel.lineBreakMode = UILineBreakModeWordWrap; cell.textlabel.lineBreakMode =UILineBreakModeTailTruncation;By truncating the label also no use.In this way my problem didnt get solved.I want all the data to be displayed in the same font size also.If the label of a cell containg less data,the font size is different and if the label of a cell containing more data,the font size is small.Please help me out.....thanks in advance
you can find the uilabels expected size programetically and also appply the word wrap..please refer this.UILabel size according to text
there are two things you need to know when wrapping text to label.
1. numberOfLine on which you want to display the text
2. lineBreakMode.
most of the time we set numberOfLine = 0 when we need to wrap the text, but keep in mind that 0 indicate , uses as many line as needed to display the text. This will cause to push the label view beyond it's container view. So make sure you have used
cell.textLabel.numberOfLines=1;
cell.textLabel.lineBreakMode = UILineBreakModeTailTruncation;