我的UILabel
有这种特殊的行为。除1外,任何numberOfLines
都可以。如果将行数设置为1,它将忽略以后设置的宽度。
我不明白为什么1条线将其拧紧...
这是我的代码
UILabel *label = [[UILabel alloc] init];
label.backgroundColor = [UIColor greenColor];
label.text = @"here is my label with lots of text to fill, here is my label with lots of text to fill";
label.frame = CGRectMake(20, 20, 100, 0);
CGRect rect = label.frame;
label.numberOfLines = 2;
label.lineBreakMode = NSLineBreakByTruncatingTail;
[self.view addSubview:label];
rect.size.width = 100;
label.frame = rect;
[label sizeToFit];
最佳答案
使用此代码:
UILabel *label = [[UILabel alloc] init];
label.backgroundColor = [UIColor greenColor];
label.text = @"here is my label with lots of text to fill, here is my label with lots of text to fill";
label.frame = CGRectMake(20, 20, 100, 0);
label.numberOfLines = 3;
label.lineBreakMode = NSLineBreakByTruncatingTail;
[self.view addSubview:label];
[label sizeToFit];
CGRect rect = label.frame;
rect.size.width = 100;
label.frame = rect;
用
numberOfLines = 3
:用
numberOfLines = 1
:
关于ios - UILabel影响边界大小的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16399963/