UIButton setAttributedTitle:forState:不显示

dav*_*Mac 8 objective-c uibutton nsattributedstring ios6

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

这是我尝试使用的代码:

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];
Run Code Online (Sandbox Code Playgroud)

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

[button setTitle:@"The Quick Brown Fox" forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)

这显示很好.有人能告诉我我错过了什么吗?

dav*_*Mac 11

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

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

[commentString addAttribute:NSForegroundColorAttributeName value:myColor  range:NSMakeRange(0, [commentString length])];
Run Code Online (Sandbox Code Playgroud)

使它正确显示.