Label in two rows

2019-08-20 10:06发布

问题:

I have a label in 2 lines in my app but the last word in the first line breaks in two rows. For example if last word is 'home' I have h in the first line and ome in the second. How can I fix that?

UPDATE:

Put over IB and in code word wrap but without success.

Code part:

...
cell.leftArticleTitleLabel.lineBreakMode = NSLineBreakByWordWrapping;

        [cell.leftArticleTitleLabel setLineBreakMode:TRUE];
        cell.leftArticleTitleView.frame = CGRectMake(cell.leftArticleTitleView.frame.origin.x, 40, 159, 40);
        cell.leftArticleTitleLabel.frame = CGRectMake(cell.leftArticleTitleLabel.frame.origin.x, cell.leftArticleTitleLabel.frame.origin.y, 149, 40);
...

UPDATE 2: Solved

I should put "setLineBreakMode:TRUE" first :-)

...
[cell.leftArticleTitleLabel setLineBreakMode:TRUE];
cell.leftArticleTitleLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.leftArticleTitleView.frame = CGRectMake(cell.leftArticleTitleView.frame.origin.x, 40, 159, 40);
cell.leftArticleTitleLabel.frame = CGRectMake(cell.leftArticleTitleLabel.frame.origin.x, cell.leftArticleTitleLabel.frame.origin.y, 149, 40);
 ...

回答1:

change the line break mode of the label to word wrapping.

graphically:

in code:

label.lineBreakMode = NSLineBreakByWordWrapping;