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

后端 未结 5 2103
醉话见心
醉话见心 2020-11-30 05:33

When myLabel.adjustsFontSizeToFitWidth = YES, UILabel will adjust the font size automatically in case the text is too long for the label. For example, if my lab

相关标签:
5条回答
  • For one-line UILabel works fine this simple solution:

    //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;
    
    0 讨论(0)
  • 2020-11-30 06:24

    In case anybody still needs the answer. In iOS9 you can use boundingRectWithSize:options:context: to calculate actual font size. Note that context.minimumScaleFactor should not be 0.0 for scaling to work.

    - (CGFloat)adjustedFontSizeForLabel:(UILabel *)label {
        NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithAttributedString:label.attributedText];
        [text setAttributes:@{NSFontAttributeName:label.font} range:NSMakeRange(0, text.length)];
    
        NSStringDrawingContext *context = [NSStringDrawingContext new];
        context.minimumScaleFactor = label.minimumScaleFactor;
        [text boundingRectWithSize:label.frame.size options:NSStringDrawingUsesLineFragmentOrigin context:context];
        CGFloat adjustedFontSize = label.font.pointSize * context.actualScaleFactor;
    
        return adjustedFontSize;
    }
    
    0 讨论(0)
  • 2020-11-30 06:24

    Swift 5

    For one-line UILabel

    extension UILabel {
    
        var actualFontSize: CGFloat {
        //initial label
         let fullSizeLabel = UILabel()
         fullSizeLabel.font = self.font
         fullSizeLabel.text = self.text
         fullSizeLabel.sizeToFit()
    
         var actualFontSize: CGFloat = self.font.pointSize * (self.bounds.size.width / fullSizeLabel.bounds.size.width);
    
        //correct, if new font size bigger than initial
         actualFontSize = actualFontSize < self.font.pointSize ? actualFontSize : self.font.pointSize;
    
         return actualFontSize
        }
    
    }
    

    Getting the actual font size is then as simple as:

    let currentLabelFontSize = myLabel.actualFontSize
    
    0 讨论(0)
  • 2020-11-30 06:27

    I'm not sure if this is entirely accurate, but it should be pretty close, hopefully. It may not take truncated strings into account, or the height of the label, but that's something you might be able to do manually.

    The method

    - (CGSize)sizeWithFont:(UIFont *)font minFontSize:(CGFloat)minFontSize actualFontSize:(CGFloat *)actualFontSize forWidth:(CGFloat)width lineBreakMode:(UILineBreakMode)lineBreakMode

    will return the text size, and notice that it also has a reference parameter for the actual font size used.

    0 讨论(0)
  • 2020-11-30 06:31
    UILabel *txtLabel = [[UILabel alloc] initWithFrame:rectMax];
    txtLabel.numberOfLines = 1;
    txtLabel.font = self.fontMax;
    txtLabel.adjustsFontSizeToFitWidth = YES;
    txtLabel.minimumScaleFactor = 0.1;
    [txtLabel setText:strMax];
    
    UILabel *fullSizeLabel = [UILabel new];
    fullSizeLabel.font = txtLabel.font;
    fullSizeLabel.text = txtLabel.text;
    fullSizeLabel.numberOfLines = 1;
    [fullSizeLabel sizeToFit];
    
    CGFloat actualFontSize = txtLabel.font.pointSize * (txtLabel.bounds.size.width / fullSizeLabel.bounds.size.width);
    actualFontSize = actualFontSize < txtLabel.font.pointSize ? actualFontSize : txtLabel.font.pointSize;
    // the actual font
    self.fontMax = [UIFont fontWithName:self.fontMax.fontName size:actualFontSize];
    

    my code works great, part from @Igor

    0 讨论(0)
提交回复
热议问题