【问题标题】:How to get UILabel (UITextView) auto adjusted font size?如何让 UILabel (UITextView) 自动调整字体大小?
【发布时间】:2011-04-09 20:23:32
【问题描述】:

自动调整后是否可以得到最终的字体大小? (属性 adjustsFontSizeToFitWidth 设置为 YES,文本字体大小正在缩小以适应标签)

我在 UILabel 中继承 drawTextInRect 以在文本上放置渐变,但渐变大小需要与字体大小相同。我无法获得调整后字体的正确大小...有可能吗?

  //draw gradient

    CGContextSaveGState(myContext);
        CGGradientRef glossGradient;
        CGColorSpaceRef rgbColorspace;
        size_t num_locations = 2;
        CGFloat locations[2] = { 0.0, 1.0 };
        CGFloat components[8] = { 1, 1, 1, 0.25,  // BOTTOM color
            1, 1, 1, 0.12 }; // UPPER color

    //scale and translate so that text would not be rotated 180 deg wrong
        CGContextTranslateCTM(myContext, 0, rect.size.height);
        CGContextScaleCTM(myContext, 1.0, -1.0);

//create mask
        CGImageRef alphaMask = CGBitmapContextCreateImage(myContext);
        CGContextClipToMask(myContext, rect, alphaMask);

        rgbColorspace = CGColorSpaceCreateDeviceRGB();
        glossGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations);

    //gradient should be sized to actual font size. THIS IS THE PROBLEM - EVEN IF FONT IS AUTO ADUJSTED, I AM GETTING THE SAME ORIGINAL FONT SIZE!!!

        CGFloat fontCapHeightHalf = (self.font.capHeight/2)+5;
            CGRect currentBounds = rect;
        CGPoint topCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMidY(currentBounds)-fontCapHeightHalf);
        CGPoint midCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMidY(currentBounds)+fontCapHeightHalf);

        CGContextDrawLinearGradient(myContext, glossGradient, topCenter, midCenter, 0);

        CGGradientRelease(glossGradient);
        CGColorSpaceRelease(rgbColorspace);
    CGContextRestoreGState(myContext);

【问题讨论】:

标签: iphone


【解决方案1】:
CGSize  size = [label.text sizeWithFont:label.font minFontSize:10 actualFontSize:&actualFontSize forWidth:200 lineBreakMode:UILineBreakModeTailTruncation];

【讨论】:

  • 这是否意味着没有使用 Swift 的解决方案?
【解决方案2】:

您无法直接获得大小,但您可以使用these functions 轻松计算:

CGFloat actualFontSize;
[label.text sizeWithFont:label.font
             minFontSize:label.minimumFontSize
          actualFontSize:&actualFontSize
                forWidth:label.bounds.size.width
           lineBreakMode:label.lineBreakMode];

【讨论】:

【解决方案3】:

我的回答对原始问题没有太大帮助,但是每次我搜索如何在自动调整后获取字体大小时,我都会到此结束。 首先,众所周知,sizeWithFont 在 iOS 7.0 中已被弃用,所以我们必须另寻解决方案。

我的解决方案适合我的情况,其中我有两个标签,一个比第二个有更多的约束和更多的文本。

这是一个分步示例:

  • 两个相同宽度和不同文本长度的标签:

我希望在运行时调整主 uilabel 的字体大小,而第二个具有相同的字体大小。

  • 减小两个标签的大小,使第一个标签的文本适合框架,在这种情况下,两个标签都为 12:

  • 设置位置约束后(在我的示例中,标签位于父视图的中心),更新两个标签的框架,使其完全适合文本:

  • 将宽度和高度之间的Aspect Ratio 约束添加到两个标签以及主标签和父视图之间的宽度约束:

  • 将第二个标签的宽度约束添加到主标签:

  • 现在您可以根据需要设置标签的字体大小:

这是模拟器的截图:

第一个标签根据屏幕大小调整大小,字体大小也随之调整。由于宽度限制和最后一张图片中指定的字体参数,第二个标签的字体大小相同。

这只是一个示例,旨在展示我的解决方案背后的“技巧”:以初始字体大小绑定标签的框架,以便在运行时保持相同。我想这种技术可以重新适应可变大小的标签,只需在设置文本并调整框架大小以适应文本后添加标签之间的约束即可。

【讨论】:

    【解决方案4】:

    这个简单的解决方案适用于单行 UILabel:

    //myLabel - initial label
    
    UILabel *fullSizeLabel = [UILabel new];
    fullSizeLabel.font = myLabel.font;
    fullSizeLabel.text = myLabel.text;
    [fullSizeLabel sizeToFit];
    
    CGFloat actualFontSize = myLabel.font.pointSize * (myLabel.bounds.size.width / fullSizeLabel.bounds.size.width);
    
    //correct, if new font size bigger than initial
    actualFontSize = actualFontSize < myLabel.font.pointSize ? actualFontSize : myLabel.font.pointSize;
    

    【讨论】:

    • 也可以用actualFontSize = actualFontSize &lt; myLabel.font.pointSize ? actualFontSize : myLabel.font.pointSize;代替actualFontSize = min(actualFontSize, myLabel.font.pointSize)
    猜你喜欢
    • 1970-01-01
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-07
    • 2014-09-25
    • 2014-01-18
    相关资源
    最近更新 更多