c2programming
c2programming

Reputation: 265

How do I display the title of a UIButton when overriding it's drawRect

How do I draw the title of a UIButton when subclassing it? I need to subclass the UIButton class because of a custom drawRect method for drawing the button.

I believe that I have to draw the title inside the drawRect method, do I see that correctly?

Upvotes: 1

Views: 945

Answers (1)

jrturton
jrturton

Reputation: 119242

It is better (and advised by Apple) not to subclass UIButton if you can. Most custom appearances can be achieved by setting a background image to a standard "custom" button. This way you still get the functionality of resizing, the label, the image etc. without needing to subclass. A button has a background image, an image and a title label - how and by what these components are drawn is not publicly available.

If the image is derived at run time, you can still use the above technique, but draw in a new graphics context, extract the image and use that instead.

If you really must use a drawRect method, I'd subclass UIControl rather than UIButton.

Upvotes: 1

Related Questions