Connor Neville
Connor Neville

Reputation: 7351

UIButton Multiple Labels Using UIControlStates

I know with a UIButton, I can add additional UILabels as subviews:

[myButton addSubview: myLabel];

And (at least, with the default title label) I can set its text color when tapped by using:

[myButton setTitleColor:someColor forState:UIControlStateHighlighted]

My question is, how can I implement this functionality for additional UILabels added to the UIButton (if this is possible)?

Upvotes: 0

Views: 496

Answers (3)

dmorrow
dmorrow

Reputation: 5664

Subclass UIButton and add your additional labels in there as instance variables. Then override -setHighlighted and -setSelected to adjust the additional labels as desired. FYI - you call [myButton setTitleColor...], not [myButton.titleLabel setTitleColor...]

Upvotes: 2

user4151918
user4151918

Reputation:

You would have to set myLabels text color, before you added it as a subView.

Otherwise, you'll have to enumerate through the button's subviews and change each of your added label's text colors.

Update:

You can change the button title's font as follows:

myButton.titleLabel!.font = UIFont(name: "...", 10)

You can change the button's title color as follows:

colorsBtn.setTitleColor(UIColor.brownColor(), forState: UIControlState.Highlighted)

Upvotes: 1

Connor Neville
Connor Neville

Reputation: 7351

It seems my way of going about it isn't easy, but I realized I can just add an action to the UIButton for the event UITouchDown, and change the labels accordingly in the action.

Upvotes: 1

Related Questions