Change a UIButton's text (padding) programmatically in Swift

IosIphoneSwiftUibutton

Ios Problem Overview


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!

Ios Solutions


Solution 1 - Ios

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) 

Solution 2 - Ios

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)

Solution 3 - Ios

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.

Solution 4 - Ios

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

Solution 5 - Ios

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()
}

Solution 6 - Ios

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()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKellysOnTop23View Question on Stackoverflow
Solution 1 - Ios7studView Answer on Stackoverflow
Solution 2 - IosDev2rightsView Answer on Stackoverflow
Solution 3 - IosAli RazaView Answer on Stackoverflow
Solution 4 - IosNike KovView Answer on Stackoverflow
Solution 5 - IosHannesView Answer on Stackoverflow
Solution 6 - IosAhmet GokdayiView Answer on Stackoverflow