【问题标题】:UIButton setAttributedTitle: forState: Not Showing UpUIButton setAttributedTitle:forState:不显示
【发布时间】:2013-08-14 14:41:16
【问题描述】:

我正在尝试在自定义 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];

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

【问题讨论】:

  • 我的回答不正确,所以我删除了它。您的目标是哪个版本的 iOS?
  • @Firo iOS 6.0 或更高版本

标签: objective-c ios6 uibutton nsattributedstring


【解决方案1】:

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

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

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

让它正确显示。

【讨论】:

  • 仅供参考,@middaparka 上面的回复与我的回答无关。这是对一位心怀不满的用户的回复,他想知道为什么他们的错误答案被否决了。
猜你喜欢
  • 1970-01-01
  • 2023-04-02
  • 2012-04-01
  • 2020-10-30
  • 1970-01-01
  • 1970-01-01
  • 2011-11-30
  • 2015-06-22
相关资源
最近更新 更多