iosuilabelnsattributedstringshrink

UILabel does not autoshrink text after assigning NSAttributedString


I have a label with a limited width, and I need it to auto-adjust the font size for the text to fit in. Since I need the text to be underlined, I assigned an attributed string to this label:

[_commentsLabel setAttributedText:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%d comments", [comments count]] attributes:@{NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle)}]];

As you can see, the number of comments will define the length of the text. But for some reason, the text does not shrink. Minimum font scale is set to 0.1 and Tighten Letter Spacing is checked.

I thought it could relate to a custom font I am using, but even with the system default font the text will be clipped.


Solution

  • I would try setting the labels property

    @property(nonatomic) BOOL adjustsFontSizeToFitWidth
    

    to YES and see if that fixes the problem. Let me know if not. I had a problem in a different situation, but i ended up using some code to change the size manually.

    Here is the code i used to change the font size manually then. Im not sure what your problem is but this ended up being a good fix for my problem. Just call this method when you set your labels text and then set the font size yourself.

        - (CGFloat)requiredFontSizeForLabel:(UILabel *)label
    {
        if (!label) {
            return kFontSize;
        }
        CGFloat originalFontSize = kFontSize;
    
        UIFont* font = label.font;
        CGFloat fontSize = originalFontSize;
    
        BOOL found = NO;
        do
        {
            if( font.pointSize != fontSize )
            {
                font = [font fontWithSize: fontSize];
            }
            if([self wouldThisFont:font workForThisLabel:label])
            {
                found = YES;
                break;
            }
    
            fontSize -= 0.5;
            if( fontSize < (label.minimumScaleFactor * label.font.pointSize))
            {
                break;
            }
    
        } while( TRUE );
    
        return( fontSize );
    }
    
        - (BOOL) wouldThisFont:(UIFont *)testFont workForThisLabel:(UILabel *)testLabel {
        NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:testFont, NSFontAttributeName, nil];
        NSAttributedString *as = [[NSAttributedString alloc] initWithString:testLabel.text attributes:attributes];
        CGRect bounds = [as boundingRectWithSize:CGSizeMake(CGRectGetWidth(testLabel.frame), CGFLOAT_MAX) options:(NSStringDrawingUsesLineFragmentOrigin) context:nil];
        BOOL itWorks = [self doesThisSize:bounds.size fitInThisSize:testLabel.bounds.size];
        return itWorks;
    }
    
            - (BOOL)doesThisSize:(CGSize)aa fitInThisSize:(CGSize)bb 
        {
            if ( aa.width > bb.width ) return NO;
            if ( aa.height > bb.height ) return NO;
            return YES;
        }
    

    Source for code found here