开发者

Change label colors of a custom UIButton with multiple labels

开发者 https://www.devze.com 2023-02-09 01:17 出处:网络
I am creati开发者_运维问答ng 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 t

I am creati开发者_运维问答ng 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?


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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消