gpt4 book ai didi

ios - 动态高度uilabel

转载 作者:行者123 更新时间:2023-12-01 17:23:18 26 4
gpt4 key购买 nike

我试图随着文本宽度的变化动态地更改uilabel的高度,但是当我尝试使用此行“white wallington hall”时出现问题。基本上,它会计算uilabel中两行的宽度,但是当它显示为两行时,它看起来像这样:

"white   
wallington h..."

样例代码:
 UILabel* lbl_Property = [[UILabel alloc]init];
[lbl_Property setFont:[UIFont fontWithName:@"Arial" size:10]];
[lbl_Property setNumberOfLines:0];
[lbl_Property setText:[arr_SubDetail objectAtIndex:x]];
UIFont* font = [UIFont fontWithName:@"Arial" size:10];
CGSize stringSize = [lbl_Property.text sizeWithFont:font];
CGFloat width = stringSize.width;


[lbl_Property setFrame:CGRectMake(lbl_Property.frame.origin.x, lbl_Property.frame.origin.y, lbl_Property.frame.size.width,5+lbl_Property.frame.size.height*(ceilf(width/110)))];

实际上,在“白色”之后有一个空格,而“wallington”不在该行中,因此换行了,但是没有显示完整的文本

如何使其显示正确?

最佳答案

试试吧....

//Calculate the expected size based on the font and linebreak mode of your label
CGSize maximumLabelSize = CGSizeMake(296,9999);

CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font
constrainedToSize:maximumLabelSize
lineBreakMode:yourLabel.lineBreakMode];

//adjust the label the the new height.
CGRect newFrame = yourLabel.frame;
newFrame.size.height = expectedLabelSize.height;
yourLabel.frame = newFrame;

关于ios - 动态高度uilabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15847919/

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