hoya21
hoya21

Reputation: 893

UILabel LineBreakMethod

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.

Upvotes: 1

Views: 107

Answers (3)

Jignesh Mayani
Jignesh Mayani

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

Raj Shekhar
Raj Shekhar

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

Dinesh
Dinesh

Reputation: 939

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

Upvotes: 0

Related Questions