1

I tried to break a label according to the text that contains. The code is below:

mnhmeioLabel.numberOfLines=0; mnhmeioLabel.lineBreakMode=NSLineBreakByWordWrapping; [mnhmeioLabel sizeToFit]; 

Although it seems to work only sometimes. I added a snapshot of my problem and what i want.enter image description here

The problem is in red box that breaks the line with a way that i cannot understand. The result that i want is in the yellow box.

9
  • make sure that you are not giving so much space between words Commented Nov 29, 2013 at 12:45
  • check that you always call [mnhmeioLabel sizeToFit] just after setting the text Commented Nov 29, 2013 at 12:45
  • @slecorne I always do this after setting the text. Commented Nov 29, 2013 at 12:48
  • @AshutoshMishra how can i check this? Text is read from json file and is fixed. Commented Nov 29, 2013 at 12:49
  • Please check Json text at your end by printing NSLOg Commented Nov 29, 2013 at 12:51

3 Answers 3

3

I think there might be a space in the text that is coming from json.

Try this:

 NSString * jsonAfterOmittingSpace=[jsonString stringByReplacingOccurrencesOfString:@" " withString:@""]; 

and then put this string on label

Sign up to request clarification or add additional context in comments.

Comments

0

Seems like your label width is changing at runtime. use a Fixed width/height for your UILabel

2 Comments

Label is in a tableview cell and it has fixed width and height.
NSLog your width and height of label and check in cellForRowAtIndexPath:.. from image it seems like width of the label is changing somehow..
-1

first YouShould get label Height base on text

UILabel *mnhmeioLabel = [[UILabel alloc] initWithFrame:giveYourframe]; [mnhmeioLabel setText:heregiveYourText] mnhmeioLabel.lineBreakMode=NSLineBreakByWordWrapping; // get height base on label text and size CGFloat height=[self getStringHeightforLabel:mnhmeioLabel]; //set fram after getting height cell.mnhmeioLabel.frame=CGRectMake(cell.mnhmeioLabel.frame.origin.x,cell.mnhmeioLabel.frame.origin.y,cell.mnhmeioLabel.frame.size.height,height); [cell.mnhmeioLabel sizeToFit]; 

// call this to get height

-(CGFloat)getStringHeightforLabel:(UILabel *)label { CGSize maximumLabelSize = CGSizeMake(label.frame.size.width,9999); CGSize stringSize= [label.text sizeWithFont:label.font constrainedToSize:maximumLabelSize lineBreakMode:label.lineBreakMode]; return stringSize.height; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.