更改具有多个标签的自定义 UIButton 的标签颜色

发布于 2024-10-15 20:15:43 字数 113 浏览 7 评论 0原文

我正在创建一个带有多个标签的自定义按钮。我希望能够更改突出显示/普通 UIControlStates 上标签的文本颜色。我知道可以根据控件状态更改标题文本颜色,但我似乎无法弄清楚按钮内的自定义标签。有什么想法吗?

I am creating a custom button with multiple labels. I want to be able to change the text color of the labels on highlight/normal UIControlStates. I know its possible to change the title text color based on control states, but I can't seem to figure it out for custom labels within the button. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不打扰别人 2024-10-22 20:15:43

感谢@Rich 的想法。我对 UIButton 进行子类化并将此代码放入新类中。

这是我使用的代码:

- (void)setHighlighted:(BOOL)bHighlighted
{
    [super setHighlighted:bHighlighted];

    if (bHighlighted) {
        [label1 setTextColor:[UIColor whiteColor]];
        [label2 setTextColor:[UIColor whiteColor]];
    }else {
        [label1 setTextColor:[UIColor blackColor]];
        [label2 setTextColor:[UIColor blackColor]];
    }
}

Thanks for the idea @Rich. I subclassed UIButton and put this code in the new class.

Heres the code I used:

- (void)setHighlighted:(BOOL)bHighlighted
{
    [super setHighlighted:bHighlighted];

    if (bHighlighted) {
        [label1 setTextColor:[UIColor whiteColor]];
        [label2 setTextColor:[UIColor whiteColor]];
    }else {
        [label1 setTextColor:[UIColor blackColor]];
        [label2 setTextColor:[UIColor blackColor]];
    }
}
橘虞初梦 2024-10-22 20:15:43

听起来你想使用一些继承的 UIControl 方法(例如 sendAction:to:forEvent:) 这提供了比 UIButton 更好的控制。

Sounds like you want to use some of the inherited UIControl methods (such as sendAction:to:forEvent:) This offers even finer control than with UIButton.

晨与橙与城 2024-10-22 20:15:43

这有点复杂,但我有一本讨论这个问题的书,叫做《高级 iOS 4 开发》。基本上你必须使用运行时来交换 setHighlighted 方法。

编辑:从技术上讲,它适用于 tableviewcells,但它位于 这个在表格视图下变白

It's kinda complicated, but I have a book that talks about this it's called Advanced iOS 4 development. basically you have to use the runtime to swap out the setHighlighted method.

Edit: it's technically for tableviewcells but its in this under table view whiten

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文