gpt4 book ai didi

iphone - UILabel 大小适合

转载 作者:行者123 更新时间:2023-12-03 18:41:15 25 4
gpt4 key购买 nike

我遇到了涉及 UILabelsizeToFit 方法的问题:

UILabel *questionLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,0,320,320)];
questionLabel.lineBreakMode = UILineBreakModeWordWrap;
questionLabel.backgroundColor=[UIColor clearColor];
questionLabel.textAlignment=UITextAlignmentLeft;
questionLabel.textColor=[UIColor blackColor];
questionLabel.tag=1;
questionLabel.font=[UIFont systemFontOfSize:13];
questionLabel.numberOfLines = 0;
[questionLabel sizeToFit];
[myView addSubview:questionLabel];

我编写了这段代码来显示我的数据。但如果我写: [questionLabel sizeToFit] 我的数据无法正确显示。如果我删除 [questionLabel sizeToFit] 那么它会显示,但只显示一半的数据。

感谢和问候。

最佳答案

NSString *yourString = @"write your label text here";
CGSize s = [yourString sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(width, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
questionLabel.frame = CGRectMake(0, 0, s.width, s.height);

检查是否有帮助。

关于iphone - UILabel 大小适合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10943935/

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