UIButton setAttributedTitle:forState:不显示

时间:2013-08-14 14:41:17

标签: objective-c ios6 uibutton nsattributedstring

我正在尝试在自定义UIButton上为我的标题添加下划线,但是按照找到here的示例,我无法在屏幕上显示它。

以下是我尝试使用的代码:

NSMutableAttributedString *commentString = [[NSMutableAttributedString alloc] initWithString:@"The Quick Brown Fox"];

[commentString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, [commentString length])];

[button setAttributedTitle:commentString forState:UIControlStateNormal];

这没有显示出来。但是,如果我只是做一个常规的,就像这样:

[button setTitle:@"The Quick Brown Fox" forState:UIControlStateNormal];

这显示罚款。有人能告诉我我错过了什么吗?

1 个答案:

答案 0 :(得分:11)

我在这里意识到了这个问题。我以为它没有出现,因为我看不到它。然而,它出现了,它只是与背景颜色相同。

我错误地认为[button setTitleColor:myColor forState:UIControlStateNormal];将是attributionTitle也会使用的颜色。但是,我的背景为黑色,NSMutableAttributedString必须默认为黑色。

[commentString addAttribute:NSForegroundColorAttributeName value:myColor  range:NSMakeRange(0, [commentString length])];

使其正确显示。