ios - UILabel影响边界大小的行数

标签 ios uilabel

我的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/

相关文章:

ios - 更改 UILabel 文本导致 ScrollView 跳转

ios - UILabel:2 行文本收缩

iphone - 展开部分 UITableview 在 reloadSections 时崩溃

ios - NSMutableDictionary 更新在 UItableview 中不起作用

ios - 容器 View Controller 中的自定义 topLayoutGuide 长度

ios - 如何更改 UIAlertController 的角半径

ios - 快速更改 MTMathUILabel 的文本

ios - 如何以编程方式更改detailTextLabel高度

ios - 从 (_,_,_) 类型的抛出函数 throws -> Void 到非抛出函数类型 (NSData?, NSURLResponse?, NSError?) -> Void 的无效转换

ios - 两侧带有水平线的 Swift UILabel