How To Change Text Or Title Of UIButton in Swift Programmatically

By Eddy Chung

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, you'll love my free guide: Secrets To iOS Development. Speed up your learning curve - hundreds of students have already downloaded. Thanks for reading!
Profile Picture of Eddy Chung

Eddy Chung

I am a professional iOS developer in Silicon Valley. I teach iOS development on ZeroToAppStore.com. If you'd like to learn more about me click here or you can contact me at: eddy@zerotoappstore.com

Similar Posts