gpt4 book ai didi

objective-c - UILabel 不会调整大小

转载 作者:行者123 更新时间:2023-12-03 17:51:29 26 4
gpt4 key购买 nike

我的 View 中有 UILabel,它应该根据内容调整自身大小,但它没有这样做。我里面有多行内容。我尝试了 [label sizeToFit] + label.numberOfLines= 0

我对 UILabel 没有限制。

我也尝试使用 UITextView 代替,但字体只是停留在 13,而不是我想要的 17。你能帮助我吗?

这是现在可以运行的代码:

UILabel *textLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(20, 158, self.view.bounds.size.width-40, 550)];
textLabel2.text = textString;
textLabel2.font = [UIFont fontWithName:@"Helvetica Neue" size:17];
textLabel2.numberOfLines = 0;
[textLabel2 sizeToFit];
[self.scroller addSubview:textLabel2];

最佳答案

如果您以编程方式创建 UILabel:

此代码首先设置UIlabel的文本,然后获取文本的宽度

UILabel *label = [[UILabel alloc] init];
//set label backgroundColor so you can see the label width changes according to text
[label setBackgroundColor:[UIColor redColor]];
//edit you label text
[label setText:@"very longgggg text"];

//get width of you text and set you font size to 17.0
CGFloat width = [label.text sizeWithFont:[UIFont systemFontOfSize:17.0f]].width;

//set you label frame
label.frame = CGRectMake(10, 200, width, 30);

//add label to you view
[self.view addSubview:label];

如果您不以编程方式创建 UILabel,请删除 alloc init 行并将您的导出连接到 xib 或 Storyboard。

关于objective-c - UILabel 不会调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25448267/

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