lavoy
lavoy

Reputation: 1926

Change label colors of a custom UIButton with multiple labels

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?

Upvotes: 1

Views: 2881

Answers (3)

lavoy
lavoy

Reputation: 1926

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

Upvotes: 2

Rich
Rich

Reputation: 1090

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

Upvotes: 0

jakeva
jakeva

Reputation: 2835

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.

Upvotes: 1

Related Questions