Search code examples
ioscocoa-touchnsattributedstring

NSAttributedString, TTTAttributedLabel: Underline doesn't work with Autoshrink


I'm trying to underline a label using TTTAttributedLabel with the following code:

[self.opponentUsername setText:self.opponentUsername.text afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
    [mutableAttributedString addAttribute:(NSString *)kCTUnderlineStyleAttributeName value:[NSNumber numberWithInt:1] range:(NSRange){0, [mutableAttributedString length]}];
    return mutableAttributedString;
}];

It works fine when the Autoshrink is disabled in Interface Builder. However, as soon as I enable Autoshrink, the label loses the white color I have applied in IB and the underline disappears completely.

What am I doing something wrong here? TIA

FYI, answers which offer solutions that use some custom library other than TTTAttributedLabel are fine too.


Solution

  • You are fine.

    There is an issue with TTTAttributedLabel which when it shrink the font size, it removed any other attributes applied.

    There is a fix for that that will preserve the other attributes while adjusting the size.

    Here is my fix:

    https://github.com/mattt/TTTAttributedLabel/pull/187