ksn*_*ksn 7 iphone uilabel uifont ios
我有一个UILabel,他的文字大小有属性
title.adjustsFontSizeToFitWidth = YES;
Run Code Online (Sandbox Code Playgroud)
这阻止我使用标准方法来调整UILabel的大小.我在这里读到另一篇文章,我应该使用这个功能
sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode
Run Code Online (Sandbox Code Playgroud)
从这个答案:当-adjustsFontSizeToFitWidth设置为YES时,如何计算UILabel的字体大小?
现在,我无法弄清楚如何使它工作..这是实际的代码
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);
Run Code Online (Sandbox Code Playgroud)
UILabel错误地调整了大小,好像字体大小仍然是200 ..任何线索?谢谢!
我有一些你可以在我的github上使用的代码,检查它,它是UILabel的一个类别,你需要设置框架宽度,当你可以在UILabel上调整resizeToFit时,它调整高度以适应内容,并返回y标签末尾的位置,以便您可以调整其后出现的任何内容.
https://gist.github.com/1005520
我建议将此归档为错误.
返回的大小-sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:具有正确的宽度,但不是高度不考虑实际的字体大小.
似乎UILabel也有这个错误.更改标签的大小以匹配返回大小的字体中文本的高度-sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:将错误地将文本垂直放置在标签内.
解决方法是计算正确的高度,并将标签上的字体更改为实际的字体大小:
CGFloat pointSize = 0.0f;
CGRect frame = title.frame;
frame.size = [title.text sizeWithFont:font
minFontSize:title.minimumFontSize
actualFontSize:&pointSize
forWidth:width
lineBreakMode:title.lineBreakMode];
UIFont *actualFont = [UIFont fontWithName:@"Marker Felt" size:pointSize];
CGSize sizeWithCorrectHeight = [title.text sizeWithFont:actualFont];
frame.size.height = sizeWithCorrectHeight.height;
title.frame = frame;
title.font = actualFont;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
27785 次 |
| 最近记录: |