UIlabel text truncation

I have one UITableView. Moreover, each cell contains a UILabel. But the text UILabeldoes not truncate if it exceeds the frame width UILabel.

I tried the following options but nothing works.

1> set LineBreakModehow NSLineBreakByTruncatingTailandsetNumberOfLines:1

2> set LineBreakModeas NSLineBreakByWordWrapping`setNumberOfLines: 1

The storyboard also has the same settings.

Here is the code snippet:

NSString * fullName;
// FullName assignment code
UILabel *label = (UILabel *)[cell.contentView viewWithTag:100];
NSLog(@"textlabel width: %f , text size : %d ", label.bounds.size.width,fullName.length);
label.adjustsFontSizeToFitWidth = NO;
label.text = fullName;
[label setLineBreakMode:NSLineBreakByTruncatingTail];
[label setNumberOfLines:1];
[label sizeToFit];
[label setTextColor:[UIColor whiteColor]];
+4
source share
2 answers

This is because you are executing sizeToFit, which adjusts the width UILabelsaccording to its contents.

: , IBOutlet? , UILabel ?

+2

,

[label setAutoresizingMask:UIViewAutoresizingNone];
0

Source: https://habr.com/ru/post/1541757/


All Articles