gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 19:11:19 25 4
gpt4 key购买 nike

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

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