Zack Shapiro
Zack Shapiro

Reputation: 6998

How do I make my UIButton title have some padding?

Let's say I have a UIButton with a size of 120 wide by 30 high. I set an attributed title on it with centered text and I set a background color on the text of the attributed text for the .highlighted state.

When the user highlights the button, I want the background that shows around the title to have the same frame as the button.

I've tried positive and negative values for the button's titleEdgeInsets and contentEdgeInsets with no luck. Any suggestions here? Thanks

Upvotes: 0

Views: 80

Answers (1)

matt
matt

Reputation: 534885

The easiest (meaning most controllable) way in my opinion is to use a button subclass and override titleRect. It's called when highlighted and when unhighlighted so you get a chance to change your answer depending on the state.

However, that won't change the size of the background color of attributed text, because it applies only to the text. You'd need to change the background color of the title label itself.

Upvotes: 0

Related Questions