ios 根据内容调整 UILabel 的大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7242608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:18:58  来源:igfitidea点击:

Resize UILabel based on content

iosiphoneuilabeluifont

提问by ksn

I have a UILabel, his text size has the property

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

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

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

sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode

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

来自这个答案:当 -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);

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

UILabel 被错误地调整大小,好像字体大小仍然是 200 .. 任何线索?谢谢!

采纳答案by lemnar

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.

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

It seems likely that UILabelalso 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.

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

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;

回答by Daniel

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.

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

https://gist.github.com/1005520

https://gist.github.com/1005520

回答by Nekto

Try to create fontwith smaller fontSize. For example:

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

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

but to actualFontSizepass link to CGFloat == 200.

但实际字体大小传递链接到 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;

回答by shivangi

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

我认为您应该使用此代码,我正在使用滚动视图上的标签作为大文本,您也可以这样做

回答by Sankalp S Raul

Have you tried intrinsicContentSize?

您是否尝试过内在内容大小?

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