Reputation: 893
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.
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.
Upvotes: 1
Views: 107
Reputation: 7193
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;
}
Upvotes: -1
Reputation: 714
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
Upvotes: 3
Reputation: 939
Seems like your label width is changing at runtime. use a Fixed width/height for your UILabel
Upvotes: 0