简体   繁体   中英

How to add extra space to the edges of the text of a UIButton?

I have a UIButton with the text "Hello".

I'm using autolayout, I didn't set any width or height for the button as I want it to be as big as its content as this one might change.

And I want to add a gap of let's say 10 pts on each edge.

All the topics I have found so far talk about adding some gap by using titleEdgeInsets . I cannot use it as it will just move the title but won't resize the actual button.

Is there an easier way than: - get the text, calculate the size of its bounding rectangle - set the button's frame based on the previous size + any gap - play with titleEdgeInsets

?

If you want to add gaps outside the button then just wrap it inside a custom view, which implements systemLayoutSizeFittingSize function and returns the value returned from UIButton.systemLayoutSizeFittingSize + gap.

To add the gaps inside the button, subclass UIButton and override systemLayoutSizeFittingSize.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM