代码之家  ›  专栏  ›  技术社区  ›  Vilém Kurz

如何获得uilabel(uitextview)自动调整的字体大小?

  •  21
  • Vilém Kurz  · 技术社区  · 15 年前

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

    我正在uilabel中对drawtextinect进行子类化,以便对文本进行渐变,但渐变大小需要与字体大小相同。我无法得到适当的字体大小调整…这是可能的吗?

      //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);
    
    4 回复  |  直到 9 年前
        1
  •  17
  •   Anomie    15 年前

    你不能直接得到尺寸,但是你可以很容易地用 these functions :

    CGFloat actualFontSize;
    [label.text sizeWithFont:label.font
                 minFontSize:label.minimumFontSize
              actualFontSize:&actualFontSize
                    forWidth:label.bounds.size.width
               lineBreakMode:label.lineBreakMode];
    
        2
  •  2
  •   Viktor Apoyan    15 年前
    CGSize  size = [label.text sizeWithFont:label.font minFontSize:10 actualFontSize:&actualFontSize forWidth:200 lineBreakMode:UILineBreakModeTailTruncation];
    
        3
  •  2
  •   Sebastiano    11 年前

    我的回答对最初的问题没有太大帮助,但每次我搜索如何在自动调整后获得字体大小时,我都会到此结束。 首先, sizewithfont 已经在iOS 7.0中被弃用,我们都知道,因此我们必须找到另一个解决方案。

    我的解决方案适合我的情况,在这种情况下,我有两个标签,一个有更多约束,另一个比第二个更文本。

    下面是一个循序渐进的示例:

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

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

    • 减小两个标签的大小,使第一个标签的文本适合框架,在本例中,两个标签的文本大小都为12:

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

    • 将宽度和高度之间的 宽高比约束添加到两个标签,并将宽度约束添加到主标签和父视图之间:

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

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

    下面是模拟器的屏幕截图:

    第一个标签根据屏幕大小调整大小,因此调整字体大小。第二个标签的字体大小是相同的,这是由于宽度限制和在最后一个图像中指定的字体参数。

    这只是一个旨在展示我的解决方案背后的“技巧”的示例:将标签的框架绑定到初始字体大小,以便在运行时保持相同。我认为,只要在设置文本和调整帧大小以适应文本之后,在标签之间添加约束,这种技术就可以重新适应具有可变大小的标签。 时间我搜索如何获得字体大小后,自动调整。 首先, sizeWithFont 已经在iOS 7.0中被弃用,我们都知道,因此我们必须找到另一个解决方案。

    我的解决方案适合我的情况,在这种情况下,我有两个标签,一个有更多的约束,比第二个有更多的文本。

    下面是一个循序渐进的例子:

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

    enter image description here

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

    • 减小两个标签的大小,使第一个标签的文本适合框架,在本例中,两个标签的文本都为12:

    enter image description here

    enter image description here

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

    enter image description here

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

    enter image description here

    enter image description here

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

    enter image description here

    enter image description here

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

    enter image description here

    下面是模拟器的屏幕截图:

    enter image description here

    第一个标签根据屏幕大小调整大小,因此调整字体大小。第二个标签的字体大小是相同的,这是由于宽度限制和最后一个图像中指定的字体参数。

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

        4
  •  0
  •   Igor    9 年前

    此简单解决方案适用于一行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;