根据内容调整 UILabel 的大小

发布于 2024-12-02 05:41:19 字数 1321 浏览 3 评论 0原文

我有一个 UILabel,他的文本大小具有

title.adjustsFontSizeToFitWidth = YES;

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

sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode

这个答案中的函数: 当 -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);

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

I have a UILabel, his text size has the property

title.adjustsFontSizeToFitWidth = YES;

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

from this answer: How to figure out the font size of a UILabel when -adjustsFontSizeToFitWidth is set to YES?

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);

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

夏了南城 2024-12-09 05:41:19

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

https://gist.github.com/1005520

I have some code you could use on my github, check it out, it's a category for UILabel, you need to set the frame width and when you can resizeToFit on the UILabel, it adjusts the height to fit the content, and returns the y position of the end of the label so you can adjust any content appearing after it.

https://gist.github.com/1005520

淡紫姑娘! 2024-12-09 05:41:19

我建议将此作为错误提交。

-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;

I'd suggest filing this as a bug.

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.

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;
不及他 2024-12-09 05:41:19

尝试创建较小字体的字体。例如:

UIFont *font = [UIFont fontWithName:@"Marker Felt" size:20];

但是要 actualFontSize 将链接传递给 CGFloat == 200。

更新:

然后尝试以下操作:

UIFont *font = [UIFont fontWithName:@"Marker Felt" size:20];
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);
font = [UIFont fontWithName:@"Marker Felt" size:200];
title.font = font;

Try to create font with smaller fontSize. For example:

UIFont *font = [UIFont fontWithName:@"Marker Felt" size:20];

but to actualFontSize pass link to CGFloat == 200.

UPDATED:

try this then:

UIFont *font = [UIFont fontWithName:@"Marker Felt" size:20];
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);
font = [UIFont fontWithName:@"Marker Felt" size:200];
title.font = font;
望笑 2024-12-09 05:41:19
UILabel * label = [[UILabel alloc] init];
        [label setNumberOfLines:0];
        label.text=[detailDict valueForKey:@"method"];
        [label setFont:[UIFont fontWithName:@"Georgia" size:16.0]];
        CGSize labelsize=[label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(250, 1000.0) lineBreakMode:UILineBreakModeWordWrap];
        int y=0;
        label.frame=CGRectMake(38, y, 245, labelsize.height);
        [label setBackgroundColor:[UIColor clearColor]];
        [label setTextColor:[UIColor whiteColor]];
        scrollView.showsVerticalScrollIndicator = NO;
        y+=labelsize.height;
        [scrollView setContentSize:CGSizeMake(200,y+50)];
        [scrollView addSubview:label];
        [label release];

我认为你应该使用这段代码,我正在使用滚动视图上的标签来显示大文本,你也可以这样做

UILabel * label = [[UILabel alloc] init];
        [label setNumberOfLines:0];
        label.text=[detailDict valueForKey:@"method"];
        [label setFont:[UIFont fontWithName:@"Georgia" size:16.0]];
        CGSize labelsize=[label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(250, 1000.0) lineBreakMode:UILineBreakModeWordWrap];
        int y=0;
        label.frame=CGRectMake(38, y, 245, labelsize.height);
        [label setBackgroundColor:[UIColor clearColor]];
        [label setTextColor:[UIColor whiteColor]];
        scrollView.showsVerticalScrollIndicator = NO;
        y+=labelsize.height;
        [scrollView setContentSize:CGSizeMake(200,y+50)];
        [scrollView addSubview:label];
        [label release];

I think you should use this code and i am using the label on scrollview for a big text you can also do so

画尸师 2024-12-09 05:41:19

您尝试过intrinsicContentSize吗?

    myLable.numberOfLines = 0
    myLable.frame = CGRect(x: 10, y: 10, width: 300, height: lblSuperscript.intrinsicContentSize().height)

Have you tried intrinsicContentSize?

    myLable.numberOfLines = 0
    myLable.frame = CGRect(x: 10, y: 10, width: 300, height: lblSuperscript.intrinsicContentSize().height)
深府石板幽径 2024-12-09 05:41:19

这段代码对我有用

    questionLabel.numberOfLines = 0
    questionLabel.sizeToFit()
    questionLabel.layoutIfNeeded()

this code worked for me

    questionLabel.numberOfLines = 0
    questionLabel.sizeToFit()
    questionLabel.layoutIfNeeded()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文