67

Still learning Swift and don't know Objective-C. I saw that in order to changing a button's text programmatically requires the use of titleEdgeInsets but I am not really sure how to use it.

I would like to change the text in the button (padding) in the bottom and both the left and the right.

Thanks for any responses and/or examples!

8 Answers 8

172

Still valid for: iOS 12/Xcode 10/Swift 4.2/OSX 10.13.2


iOS 9.1/Xcode 7.1/Swift 2.1/OSX 10.10.5:

The method titleEdgeInsets didn't work for me. My button's frame tightly hugs the text (I didn't specify a frame size), and the frame has a red background color. After doing:

 myButton.titleEdgeInsets = UIEdgeInsetsMake(10,10,10,10)

the red background shrunk inwards by 10 pixels on each side, which meant that now some of the text was outside the red background. Using negative values had no effect on the original frame size.

I was able to get padding around the text, effectively making the frame bigger, by doing:

myButton.contentEdgeInsets = UIEdgeInsetsMake(5,5,5,5) 
5
  • 1
    @Nik Kov, Thanks.
    – 7stud
    Oct 25, 2018 at 21:47
  • 2
    new method is called: UIEdgeInsets.init(top: #, left:#, :bottom: #, right: #) Jun 12, 2019 at 17:51
  • 1
    @Andrea, UIEdgeInsetsMake() is still in the docs: developer.apple.com/documentation/uikit/…
    – 7stud
    Jun 12, 2019 at 20:21
  • 2
    @Andrea, Do you get a warning if you call UIEdgeInsetsMake(5,5,5,5) ?
    – 7stud
    Jun 13, 2019 at 19:19
  • 4
    titleEdgeInsets is deprecated in iOS 15 :(
    – Raildex
    Nov 15, 2021 at 9:17
64

iOS 10 and Swift 3 example

Tested and works on IOS 11 and Swift 4.

refineButton.contentEdgeInsets = UIEdgeInsets(top: 5, left: 0, bottom: 0, right: 0)
2
  • 5
    If the text is truncating, while use of titleEdgeInsets, replace it to contentEdgeInsets.
    – Nike Kov
    Oct 24, 2018 at 21:59
  • Note, contentEdgeInsets is deprecated in iOS 15
    – dwu39
    Aug 1 at 4:41
32

You can add padding from top, left, right and bottom by doing these lines of code.

button.titleEdgeInsets.left = 10; // add left padding.
button.titleEdgeInsets.right = 10; // add right padding.
button.titleEdgeInsets.top = 10; // add top padding.
button.titleEdgeInsets.bottom = 10; // add bottom padding.
27

You can also do this in the storyboard:
enter image description here

4
  • 2
    The question specified "programmatically"
    – Ahmed Eid
    Jan 19, 2018 at 8:07
  • 7
    I don't pretend to a best answer. Just for fullness of information.
    – Nike Kov
    Jan 19, 2018 at 8:19
  • 6
    It was useful, @aeid sometimes I search "programmatically" because I couldn't find it in the storyboard Apr 9, 2018 at 22:45
  • I didn't search for programmatically or storyboard, because I don't care either way. Glad to know it's there
    – froggomad
    Jul 9, 2019 at 15:06
6

For iOS 9.1/Xcode 7.1/Swift 2.1

@IBOutlet weak var ratingButton: UIButton!

override func viewDidLoad() {
    ratingButton.contentEdgeInsets = UIEdgeInsets(top:15,right:10,bottom:15,left:10)

    super.viewDidLoad()
}
0

With iOS 15 contentEdgeInsets is deprecated so you need to use the following...

var configuration = UIButton.Configuration.plain() // there are several options to choose from instead of .plain()
configuration.contentInsets = NSDirectionalEdgeInsets(top: 24, leading: 24, bottom: 24, trailing: 24)
button.configuration = configuration
-1

Or you can use a custom class if you don't want to define the properties everytime you create a button;

class UIPaddedButton: UIButton {
  let padding = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)

  required init?(coder: NSCoder) {
    super.init(coder: coder)
  }

  override init(frame: CGRect) {
    super.init(frame: frame)
  }

  override func titleRect(forContentRect contentRect: CGRect) -> CGRect {
    return contentRect.inset(by: padding)
  }
}

And when initializing;

let btn = UIPaddedButton()
1
-2

For xcode 13.4 and iOS 12 Tested and working To make space to both sides of a button

ButtonName.contentEdgeInsets = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
1
  • unfortunately completely wrong, deprecated away :/
    – Fattie
    Dec 15, 2022 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.