I want to display 2 strings (at different positions in the button) with different fonts and colors (I'm using the button's setTitle
for one, and I need another one), and some attributes must be changed based on the current UIControlState
(like the color).
So, I'm searching the best way to add a second label to a UIButton
.
I need the label to be configurable per UIControlState
(I want a different color for UIControlStateNormal
and UIControleStateHighlighted
for example).
I've tried the following approches:
- Subclass a
UIButton
and usedrawRect
: while not recommended (and I now understand why), I don't think it's even possible, it looks like the button'sdrawRect
method is called (and after the one of my subclass) even if I don't call super. - Create a new
UILabel
and add it as asubview
to my button: this is working quite well, except I don't know how to change the color when theUIControlState
of the button is changing - Create a new layer and use
drawLayer
: I don't know how to get thedrawLayer
method to be called every time the button state is changing (mydrawLayer
only gets called once, when I usesetNeedsDisplay
just after adding my layer to the button)
Is there another way to achieve what I'm trying to do, or maybe one of those solutions might work (with a few tweaks)?
Thanks!