gpt4 book ai didi

iOS sizeToFit 不显示文本

转载 作者:行者123 更新时间:2023-11-29 03:10:26 28 4
gpt4 key购买 nike

我正在为 Storyboard View 创建这些标签。我将自动布局设置为“关闭”,并将行数设置为 0,但仅当我注释掉 sizeToFit 时才会显示文本。当然,当文本高度大于 40 时,文本就会被截断。

- (void)viewDidLoad
{

[super viewDidLoad];
first = @"This text fits in the label";
second = @"This large text is too large for the label but because the words are normal sized, shows the more button correctly at the bottom right";
third = @"Doesn't show the more button correctly because WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW";

firstlbl = [[UILabel alloc]initWithFrame:CGRectMake(13, 57, 295, 40)];
secondlbl = [[UILabel alloc]initWithFrame:CGRectMake(13, 152, 295, 40)];
thirdlbl = [[UILabel alloc]initWithFrame:CGRectMake(13, 225, 295, 40)];

[firstlbl setFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:14]];
[secondlbl setFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:14]];
[thirdlbl setFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:14]];

firstlbl.lineBreakMode = NSLineBreakByWordWrapping;
secondlbl.lineBreakMode = NSLineBreakByWordWrapping;
thirdlbl.lineBreakMode = NSLineBreakByWordWrapping;

firstlbl.numberOfLines = 0;
secondlbl.numberOfLines = 0;
thirdlbl.numberOfLines = 0;
[firstlbl sizeToFit];
[secondlbl sizeToFit];
[thirdlbl sizeToFit];

[firstlbl setText:first];
[secondlbl setText:second];
[thirdlbl setText:third];

[self.view addSubview:firstlbl];
[self.view addSubview:secondlbl];
[self.view addSubview:thirdlbl];

}

最佳答案

您在设置文本之前调用了 sizeToFit,因此它的尺寸缩小到 CGRectZero。切换这些调用,它就会起作用。

sizeToFit 的意思不是“自动调整尺寸以适应内容”,而是“立即更新尺寸以适应内容”。对文本的后续更新不会更改框架。

如果您想要前一种行为,以便它自动根据其内容调整自身大小,自动布局可能是最简单的方法。

关于iOS sizeToFit 不显示文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22305112/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com