gpt4 book ai didi

objective-c - UIButton setAttributedTitle : forState: Not Showing Up

转载 作者:太空狗 更新时间:2023-10-30 03:31:59 25 4
gpt4 key购买 nike

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

这显示很好。有人可以告诉我我缺少什么吗?

最佳答案

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

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

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

使其正确显示。

关于objective-c - UIButton setAttributedTitle : forState: Not Showing Up,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18234797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com