Reputation: 1245
I had two labels label1 and label2 and a tableview ,when the user click the tableview the content of the tableview is displayed in two labels.means if the tableview cell contain stack and 1 ,1 in the text-label.text. i need to display stack in the label1 and 1 in the label2 ,i have done this .but the problem is when the tableview cell content is big i.e.,instead of stack there is stackoverflow it was overlapped with label2 due to big string content For example stack:1 is ok but when the content is stackoverflow it will appears stackoverflow1 or stackove...1 something like that.so My need is label2 must change its position according to the string length of label1.How to do this?.i hope u understand my question. Thanks in advance.
Upvotes: 0
Views: 604
Reputation: 4732
you can set any frame you your label when you setting new text to it:
[yourLabel setFrame: CGRectMake(x, y, w, h)];
just calculate it according length of new text. But... do you REALLY need 2 labels? If thay placed in one line and you do so only becouse of you have 2 words, you may do this much easer: Use 1 label with widgh of all screen and set new text like this:
[yourLabel setText:[NSString stringWithFormat(@"%@ %@", text1, text2)]];
Upvotes: 1
Reputation: 1246
You can get string size with the help of following code
CGSize stringSize = [myString sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE]];
Now you can get frame of the first label and then its text size as well. I hope now it will be easy for you to adjust the postion of second label. In case of any problem let me know. Here is my code for getting number of lines of a label so that I can get the first label's height to add the second label after it.
CGSize stringSize = [myString sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE]];
int labelwidth = firstLabel.width;
int number_of_lines = stringSize.width/labelwidth;
int size = stringSize.width;
int reminder = size % labelwidth;
if(reminder != 0){
number_of_lines++;
}
// now first label height will be
rowHeight = number_of_lines*FONT_SIZE + GAP_BETWEEN_ROWS
Now you can use this rowHeigh as frame.origin.y of your second label.
Best of luck
Upvotes: 2
Reputation: 991
Another way to achieve this if you want show on two separate labels you have to calculate the width of first label according to you text set the frame of first label and then add say 5px to the width of first label this will become x position of your next label.
Upvotes: 0