UIButton setAttributedTitle:forState:不显示 [英] UIButton setAttributedTitle: forState: Not Showing Up

查看:1083
本文介绍了UIButton setAttributedTitle:forState:不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在自定义UIButton上为我的标题添加下划线,但是按照此处的示例,我可以不要让它显示在屏幕上。

I am trying to add underlining to my title on a custom UIButton but by following the example found here, I can't get it to show on the screen.

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

Here is the code I am trying to use:

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

这不显示。但是,如果我只是做一个常规的,如下:

This doesn't show up. But if I just do a regular one, like so:

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

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

This shows up fine. Can someone tell me what I am missing?

推荐答案

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

I realized the problem here. I assumed that it was not showing up because I could not see it. However, it was showing up, it is just the same color as the background.

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

I wrongly assumed that [button setTitleColor:myColor forState:UIControlStateNormal]; would be the color that the attributedTitle would use as well. However, my background is black and the NSMutableAttributedString must default to black.

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

使其正确显示。

这篇关于UIButton setAttributedTitle:forState:不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆