titleEdgeInsets = UIEdgeInsets ( top : 0, left : -imageTitlePadding, bottom : 0, right : imageTitlePadding ) left + imageTitlePadding, bottom : contentPadding. contentEdgeInsets = UIEdgeInsets ( top : contentPadding. In RTL environments, it's necessary to adjust the setInsets method as such: self. In locales which write right-to-left, content and image insets have their right and left insets reversed, since the image is to the right of the text. Once we know how to do this, we can extract it to a method: extension UIButton Right-to-left languages With negative contentEdgeInsets, it's possible to get the image and text to draw outside of the button's bounds. Negative values contract the button towards its content.Positive values expand the button away from its content.The insets apply padding to the content of the button, and they're taken into account in the button's instrinsicContentSize (which was what I expected). contentEdgeInsetsĬontentEdgeInsets is probably the easiest insets property to reason about. To understand why adding image-title padding is hard, we have to look at UIButton's various insets properties, which control how the UIButton is drawn (in different ways). There are several posts talking about this topic (including this one which uses edge insets to flip the title and image!), but the answer for my use case was pretty hard to find.Īdding image-title padding is possible, but takes some trickiness with contentEdgeInsets, titleEdgeInsets, and imageEdgeInsets. I had to do this at work recently, and I was surprised at how hard it was to reason about. If you're looking for a code snippet, skip to the bottom :)
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |