How To Change Text Or Title Of UIButton in Swift Programmatically

Sometimes you’ll need to change the title or text of a button.

How To Change Text or Title Of UIButton in Swift Programmatically

Call the setTitle function like so:

button.setTitle("Ok", for: .normal)

Be sure that your button has already be defined before calling this function.

If you’re using storyboards or .XIB files, that means you must have a @IBOutlet (not an @IBAction).

How To Set Attributed Title For A Button

To set an attributed title for a button, use this function:

button.setAttributedTitle(attributedString, for: .normal)

Note that if you set both an attributed title and a regular title, the button will prefer the attributed title.

If you liked this post and want to learn more, check out The Complete iOS Developer Bootcamp. Speed up your learning curve - hundreds of students have already joined. Thanks for reading!

Eddy Chung

I teach iOS development on ZeroToAppStore.com.

Similar Posts