根据内容调整UILabel的大小 [英] Resize UILabel based on content

查看:137
本文介绍了根据内容调整UILabel的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a UILabel, his text size has the property

title.adjustsFontSizeToFitWidth = YES;

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

that prevents me from using standard methods to resize the UILabel. I read on another post here that I'm supposed to use the function

sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode

当-adjustsFontSizeToFitWidth设置为YES时,如何计算出UILabel的字体大小?

现在,我无法弄清楚如何使它工作..这是实际的代码

Now, i can't figure out how to make it work.. this is the actual code

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 ..
Any线索?谢谢!

The UILabel get resized wrongly, as if the font size it's still 200.. Any clues? Thanks!

推荐答案

我建议将此归档为错误。

I'd suggest filing this as a bug.

-sizeWithFont返回的大小:minFontSize:actualFontSize:forWidth:lineBreakMode:具有正确的宽度,但高度不考虑实际的字体大小。

The size returned by -sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode: has the correct width, but not the height does not account for the actual font size.

似乎 UILabel 也有此错误。更改标签的大小以匹配 -sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:返回的大小字体中文本的高度将错误地垂直定位标签中的文字。

It seems likely that UILabel also has this bug. Changing the size of a label to match the height of the text in a font of the size returned by -sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode: will incorrectly vertically position the text within the label.

解决方法是计算正确的高度,并将标签上的字体更改为实际字体大小:

A work-around is to calculate the correct height, and change the font on the label to with the actual font size:

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;

这篇关于根据内容调整UILabel的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆