当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - 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.

网友答案:

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

网友答案:

AttributeStrings have their own font size(s). Do this manually.

All attributed strings that compose the attributed string must have a NSFontAttributeName.

func updateLabelSizeIfNeeded() {
    let maxScale: CGFloat = 0.65
    let bounding = self.label.attributedText!.boundingRectWithSize(CGSizeMake(CGFloat.infinity, CGFloat.infinity), options: [], context: nil)
    if bounding.size.width > self.bounds.size.width*maxScale {
        let scaleFactor = (self.bounds.size.width * maxScale) / bounding.size.width

        label.transform = CGAffineTransformMakeScale(scaleFactor, scaleFactor)
    } else {
        label.transform = CGAffineTransformIdentity
    }
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: