0

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)?

Connor Neville
  • 7,291
  • 4
  • 28
  • 44

3 Answers3

2

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...]

dmorrow
  • 5,152
  • 5
  • 20
  • 31
  • That actually makes a lot more sense than my approach. Thanks. – Connor Neville May 20 '15 at 01:47
  • If you simply want to change the title color, you don't need to add more labels. Just change the existing label's color. See my answer for details. –  May 20 '15 at 01:49
  • @PetahChristian that wasn't my question. I do want multiple labels because I want them to be formatted differently from one another. – Connor Neville May 20 '15 at 01:51
  • You want one button to show multiple labels for a single control state? –  May 20 '15 at 02:01
1

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)
  • I have the subviews added. I'd like to change each of their font colors for the control event UIControlStateHighlighted, but I realized adding an action for the button is probably a lot easier. – Connor Neville May 20 '15 at 01:38
1

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.

Connor Neville
  • 7,291
  • 4
  • 28
  • 44