NSAttributedString,TTTAttributedLabel:下划线不适用于自动收缩

时间:2013-03-07 13:37:27

标签: ios cocoa-touch nsattributedstring

我正在尝试使用TTTAttributedLabel使用以下代码为标签加下划线:

[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;
}];

在Interface Builder中禁用自动收缩时,它可以正常工作。但是,一旦启用自动收缩,标签就会丢失我在IB中应用的白色,并且下划线完全消失。

我在这里做错了什么? TIA

仅供参考,提供使用除TTTAttributedLabel之外的某些自定义库的解决方案的答案也很好。

1 个答案:

答案 0 :(得分:1)

你没事。

TTTAttributedLabel存在一个问题,当它缩小字体大小时,会删除所有其他应用的属性。

有一个修复方法可以在调整大小时保留其他属性。

这是我的修复:

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