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.
我试图在自定义UIButton上为我的标题添加下划线但是按照这里找到的示例,我无法在屏幕上显示它。
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?
这显示很好。有人能告诉我我错过了什么吗?
1 个解决方案
#1
11
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.
我在这里意识到了问题。我以为它没有出现,因为我看不到它。然而,它出现了,它只是与背景颜色相同。
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.
我错误地认为[button setTitleColor:myColor forState:UIControlStateNormal];将是attributionTitle也会使用的颜色。但是,我的背景是黑色,NSMutableAttributedString必须默认为黑色。
[commentString addAttribute:NSForegroundColorAttributeName value:myColor range:NSMakeRange(0, [commentString length])];
Makes it show up correctly.
使其正确显示。
#1
11
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.
我在这里意识到了问题。我以为它没有出现,因为我看不到它。然而,它出现了,它只是与背景颜色相同。
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.
我错误地认为[button setTitleColor:myColor forState:UIControlStateNormal];将是attributionTitle也会使用的颜色。但是,我的背景是黑色,NSMutableAttributedString必须默认为黑色。
[commentString addAttribute:NSForegroundColorAttributeName value:myColor range:NSMakeRange(0, [commentString length])];
Makes it show up correctly.
使其正确显示。