UIButton setAttributedTitle:forState:未显示

2024-03-18

我试图在自定义 UIButton 上的标题上添加下划线,但按照发现的示例here https://stackoverflow.com/a/15930032/329900,我无法让它显示在屏幕上。

这是我尝试使用的代码:

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

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


我在这里意识到了问题。我认为它没有出现,因为我看不到它。然而,它确实出现了,它的颜色与背景颜色相同。

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

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

使其正确显示。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

UIButton setAttributedTitle:forState:未显示 的相关文章

随机推荐