gpt4 book ai didi

ios - 根据内容调整 UILabel 的大小

转载 作者:可可西里 更新时间:2023-11-01 03:27:31 26 4
gpt4 key购买 nike

我有一个UILabel,他的文字大小有属性

title.adjustsFontSizeToFitWidth = YES;

这会阻止我使用标准方法来调整 UILabel 的大小。我在这里的另一篇文章中读到我应该使用该函数

sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode

来自这个答案:How to figure out the font size of a UILabel when -adjustsFontSizeToFitWidth is set to YES?

现在,我想不出如何让它工作..这是实际的代码

UIFont *font = [UIFont fontWithName:@"Marker Felt" size:200];
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, width, 20.0)];
title.text = @"this is a long title that should be resized";
title.font = font;
title.numberOfLines = 1;
title.adjustsFontSizeToFitWidth = YES;

CGFloat pointSize = 0.0;
CGSize size = [title.text sizeWithFont:font
minFontSize:title.minimumFontSize
actualFontSize:&pointSize
forWidth:width
lineBreakMode:title.lineBreakMode];
title.frame = CGRectMake(title.frame.origin.x,
title.frame.origin.y,
size.width,
size.height);

UILabel 的大小调整错误,就好像字体大小仍然是 200..有什么线索吗?谢谢!

最佳答案

我的 github 上有一些你可以使用的代码,检查一下,它是 UILabel 的一个类别,你需要设置框架宽度,当你可以在 UILabel 上调整尺寸时,它会调整高度以适合内容,并且返回标签末尾的 y 位置,以便您可以调整出现在它后面的任何内容。

https://gist.github.com/1005520

关于ios - 根据内容调整 UILabel 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7242608/

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