Programming

Aligning text and image on UIButton with imageEdgeInsets and titleEdgeInsets

25 September 2026 · 8 min read

Aligning text and image on UIButton with imageEdgeInsets and titleEdgeInsets

Aligning images and text on UIButtons is a fundamental aspect of iOS app design. Achieving that perfect harmony between visuals and words significantly impacts user experience, making your app intuitive and aesthetically pleasing. While seemingly straightforward, precise alignment can be tricky. This comprehensive guide delves into the nuances of using imageEdgeInsets and titleEdgeInsets, offering practical solutions and best practices for flawless UIButton customization.

Understanding imageEdgeInsets and titleEdgeInsets

These properties, imageEdgeInsets and titleEdgeInsets, are your primary tools for manipulating the positioning of images and text within a UIButton. They function by adding insets (inner spacing) around the content, effectively shifting its placement. Understanding how these insets work is crucial for precise control. Think of it like adding padding around a picture in a frame, allowing you to adjust its position within the frame’s boundaries.

imageEdgeInsets controls the image’s position, while titleEdgeInsets governs the text. By manipulating these insets, you can achieve various alignments, including image-left/text-right, image-top/text-bottom, and more. Remember, positive inset values increase the spacing, pushing the content inwards, while negative values decrease the spacing, potentially pushing the content outwards.

For example, to move the image 10 points to the right, you’d use button.imageEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0). Similarly, adjusting the text’s position involves modifying the titleEdgeInsets property.

Practical Alignment Scenarios

Let’s explore common alignment scenarios and how to achieve them:

Image Left, Text Right

This classic layout is common for many button styles. To achieve this, you need to add a positive left inset to the image and a negative right inset to the text, effectively pushing them apart. The exact values will depend on your button size and the desired spacing.

Image Top, Text Bottom

This vertical alignment is useful for buttons with icons or logos. Here, you’ll modify the top/bottom insets for the image and text respectively.

Centered Image and Text

Centering both elements requires careful calculation based on the image and text sizes. You’ll need to use a combination of positive and negative insets to achieve perfect centering.

Troubleshooting Common Issues

While imageEdgeInsets and titleEdgeInsets offer powerful control, they can sometimes be tricky. One common issue is content clipping when insets push content outside the button’s bounds. Ensure your insets don’t cause this. Another challenge is achieving perfect alignment across different screen sizes. Testing on various devices and using auto-layout constraints is crucial for maintaining consistency.

Consider using a dedicated layout method within your code for more complex alignments, especially when dealing with dynamic content sizes. This approach offers greater flexibility and maintainability compared to directly manipulating insets.

For instance, you could create a function that takes the image and text sizes as input and calculates the appropriate insets. This allows for dynamic adjustments based on content, ensuring consistent alignment across different scenarios.

Advanced Customization Techniques

Beyond basic alignment, you can leverage these properties for more advanced effects. For example, you can create custom button styles with unique image and text placements or implement animations by modifying the insets dynamically. Experimenting with different combinations opens up a world of possibilities for creative button design.

Using extensions in Swift can streamline the process of setting these insets, especially if you’re working with consistent alignment patterns throughout your app. This promotes code reusability and readability. For complex UI button designs, it’s worth exploring UIEdgeInsets extensions that can simplify calculations, handling cases like RTL languages where the image and text may need to be flipped automatically.

Here’s an example of how you might implement this using Swift extensions: extension UIButton { ... }. This approach allows you to encapsulate complex logic and provide easy-to-use methods for consistent styling across your application.

  • Use positive insets to push content inwards.
  • Use negative insets to push content outwards (with caution).
  1. Determine your desired alignment.
  2. Calculate the necessary insets.
  3. Apply the insets to imageEdgeInsets and titleEdgeInsets.
  4. Test on different devices and screen sizes.

Learn more about UIButton customization on Apple’s Developer Documentation.

Discover advanced techniques for iOS UI design with Ray Wenderlich tutorials.

Explore best practices for mobile UX at Nielsen Norman Group.

Further reading on Auto Layout: Auto Layout Guide.

Infographic Placeholder: Visual representation of imageEdgeInsets and titleEdgeInsets and their impact on alignment.

Frequently Asked Questions

Q: How do I handle different image sizes?

A: You may need to adjust your insets dynamically based on the image size to maintain consistent spacing.

By mastering the techniques discussed in this guide, you can significantly enhance your app’s user interface and create a more polished and professional look. This meticulous attention to detail demonstrates a commitment to user experience, setting your app apart in today’s competitive market. Remember, effective UI design is more than just aesthetics; it’s about creating an intuitive and enjoyable experience for your users. Start experimenting with imageEdgeInsets and titleEdgeInsets today to unlock the full potential of your UIButtons. Consider the impact of different alignments on user interaction and choose the approach that best suits your app’s overall design language and functionality.

  • Key takeaway 1: Precision alignment enhances user experience.
  • Key takeaway 2: imageEdgeInsets and titleEdgeInsets offer granular control.

Question & Answer :
Historic question. Note that 15 years later, .imagePadding is the space between the image and label.

enter image description here


Original question:

I would like to place an icon left of the two lines of text such that there’s about 2-3 pixels of space between the image and the start of text. The control itself is Center aligned horizontally (set through Interface Builder)

The button would resemble something like this:

| | |[Image] Add To | | Favorites | 

I’m trying to configure this with contentEdgeInset, imageEdgeInsets and titleEdgeInsets to no avail. I understand that a negative value expands the edge while a positive value shrinks it to move it closer to the center.

I tried:

[button setTitleEdgeInsets:UIEdgeInsetsMake(0, -image.size.width, 0, 0)]; [button setImageEdgeInsets:UIEdgeInsetsMake(0, button.titleLabel.bounds.size.width, 0, 0)]; 

but this doesn’t display it correctly. I’ve been tweaking the values but going from say -5 to -10 on the left inset value doesn’t appear to move it in expected manner. -10 will scoot the text all the way to the left so I expected -5 to scoot it half way from the left side but it doesn’t.

What’s the logic behind insets? I’m not familiar with image placements and related terminology.

I used this SO question as a reference but something about my values isn’t right. UIButton: how to center an image and a text using imageEdgeInsets and titleEdgeInsets?

I’m a little late to this party, but I think I have something useful to add.

Kekoa’s answer is great but, as RonLugge mentions, it can make the button no longer respect sizeToFit or, more importantly, can cause the button to clip its content when it is intrinsically sized. Yikes!

First, though,

A brief explanation of how I believe imageEdgeInsets and titleEdgeInsets work:

The docs for imageEdgeInsets have the following to say, in part:

Use this property to resize and reposition the effective drawing rectangle for the button image. You can specify a different value for each of the four insets (top, left, bottom, right). A positive value shrinks, or insets, that edge—moving it closer to the center of the button. A negative value expands, or outsets, that edge.

I believe that this documentation was written imagining that the button has no title, just an image. It makes a lot more sense thought of this way, and behaves how UIEdgeInsets usually do. Basically, the frame of the image (or the title, with titleEdgeInsets) is moved inwards for positive insets and outwards for negative insets.

OK, so what?

I’m getting there! Here’s what you have by default, setting an image and a title (the button border is green just to show where it is):

Starting image; no space between title and image.

When you want spacing between an image and a title, without causing either to be crushed, you need to set four different insets, two on each of the image and title. That’s because you don’t want to change the sizes of those elements’ frames, but just their positions. When you start thinking this way, the needed change to Kekoa’s excellent category becomes clear:

@implementation UIButton(ImageTitleCentering) - (void)centerButtonAndImageWithSpacing:(CGFloat)spacing { CGFloat insetAmount = spacing / 2.0; self.imageEdgeInsets = UIEdgeInsetsMake(0, -insetAmount, 0, insetAmount); self.titleEdgeInsets = UIEdgeInsetsMake(0, insetAmount, 0, -insetAmount); } @end 

But wait, you say, when I do that, I get this:

Spacing is good, but the image and title are outside the view’s frame.

Oh yeah! I forgot, the docs warned me about this. They say, in part:

This property is used only for positioning the image during layout. The button does not use this property to determine intrinsicContentSize and sizeThatFits:.

But there is a property that can help, and that’s contentEdgeInsets. The docs for that say, in part:

The button uses this property to determine intrinsicContentSize and sizeThatFits:.

That sounds good. So let’s tweak the category once more:

@implementation UIButton(ImageTitleCentering) - (void)centerButtonAndImageWithSpacing:(CGFloat)spacing { CGFloat insetAmount = spacing / 2.0; self.imageEdgeInsets = UIEdgeInsetsMake(0, -insetAmount, 0, insetAmount); self.titleEdgeInsets = UIEdgeInsetsMake(0, insetAmount, 0, -insetAmount); self.contentEdgeInsets = UIEdgeInsetsMake(0, insetAmount, 0, insetAmount); } @end 

And what do you get?

Spacing and frame are now correct.

Looks like a winner to me.


Working in Swift and don’t want to do any thinking at all? Here’s the final version of the extension in Swift:

extension UIButton { func centerTextAndImage(spacing: CGFloat) { let insetAmount = spacing / 2 let isRTL = UIView.userInterfaceLayoutDirection(for: semanticContentAttribute) == .rightToLeft if isRTL { imageEdgeInsets = UIEdgeInsets(top: 0, left: insetAmount, bottom: 0, right: -insetAmount) titleEdgeInsets = UIEdgeInsets(top: 0, left: -insetAmount, bottom: 0, right: insetAmount) contentEdgeInsets = UIEdgeInsets(top: 0, left: -insetAmount, bottom: 0, right: -insetAmount) } else { imageEdgeInsets = UIEdgeInsets(top: 0, left: -insetAmount, bottom: 0, right: insetAmount) titleEdgeInsets = UIEdgeInsets(top: 0, left: insetAmount, bottom: 0, right: -insetAmount) contentEdgeInsets = UIEdgeInsets(top: 0, left: insetAmount, bottom: 0, right: insetAmount) } } }