7

我有一个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 .. 任何线索?谢谢!

4

5 回答 5

8

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

https://gist.github.com/1005520

于 2011-08-30T12:16:15.103 回答
6

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

返回的大小-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;
于 2011-08-30T12:56:23.887 回答
2

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

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

实际字体大小将链接传递给 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;
于 2011-08-30T12:30:59.393 回答
0
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];

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

于 2011-08-30T12:08:24.557 回答
0

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

    myLable.numberOfLines = 0
    myLable.frame = CGRect(x: 10, y: 10, width: 300, height: lblSuperscript.intrinsicContentSize().height)
于 2015-12-29T07:08:10.057 回答