[英]UILabel Size to fit is not working
这就是我在做什么。 我不是动态创建标签,而是从对象库中选择标签。仅当我静态增加标签的高度时才起作用,但是我在运行时不知道
[self.lblDescription setText:@"This is going to be a test description but a very big description that should increase in height"];
[self.lblDescription sizeToFit];
标签不在下一行。 但是相反,它会在标签左侧到达屏幕右侧后立即对其进行裁剪
根据答案,我尝试了以下操作,但没有成功:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.lblDescription setText:@"This is going to be a test description but a very big description that should increase in size"];
self.lblDescription.numberOfLines = 0;
[self.lblDescription sizeToFit];
}
我也尝试设置-
[self.lblDescription setLineBreakMode:NSLineBreakByWordWrapping];
在调用[self.lblDescription sizeToFit];
但是没用
检查numberOfLines属性
self.lblDescription.numberOfLines = 0;
编辑 :还需要更改高度(按评论说)。
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"Helvetica Neue" size:19], NSFontAttributeName,
nil];
CGRect expectedLabelFrame = [text boundingRectWithSize:CGSizeMake(571, 500)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributesDictionary
context:nil];
// Adjust the yourLabel the the new height.
CGRect newFrame = yourLabel.frame;
newFrame.size.height = expectedLabelFrame.size.height;
yourLabel.frame = newFrame;
将adjustsFontSizeToFitWidth属性设置为true
可以帮到我。
尝试,
self.lblDescription.adjustsFontSizeToFitWidth = true
(默认情况下,它设置为false )
我发现当将sizeToFit与UILabel一起使用时,在InterfaceBuilder中,您需要将Autoshrink属性从“固定字体大小”更改为“最小字体大小”。 然后,我通常将其值设置为0.5以确保其正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.