Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UILabel truncate even when it has remaining lines

Current Behaviour

I have a multiLine UILabel with a Rectangular frame of a particular size. The font and all is so set that it will display a MAX of three lines and beyond that it will truncate.

Exceptional Behaviour

Exceptional behaviour is that sometimes I do have a complete Line or Two remaining but the line truncates. This is because the word at the end of the line is too Big to fit in the end of that line.

Expected Behaviour

I wish that the UILabel fills to all three Lines and then truncate.

This is my current code

[_detailedTextLabel setNumberOfLines:0];
[_detailedTextLabel setLineBreakMode:UILineBreakModeTailTruncation];
[_detailedTextLabel setAdjustsFontSizeToFitWidth:YES]; 
like image 249
Ganesh Somani Avatar asked Sep 13 '25 04:09

Ganesh Somani


1 Answers

Use another line breaking mode.

[_detailedTextLabel setLineBreakMode:NSLineBreakByWordWrapping];

and also ensure that label has proper height (given that width is already set):

CGSize size = [_detailedTextLabel sizeThatFits:CGSizeMake(width, CGFLOAT_MAX)];
_detailedTextLabel.frame = CGRectMake(0, 0, size.width, size.height);
like image 169
Michał Ciuba Avatar answered Sep 15 '25 17:09

Michał Ciuba