RanLearns RanLearns - 9 months ago 78
iOS Question

Swift: Remove a UIButton attributedTitle

If a UIButton has both a title and an attributedTitle set, the attributedTitle is the one that shows.

If I set a button to an attributed title:

myButton.setAttributedTitle(myAttribText, for: .normal)

and later in the app I want to set the button to a regular title:

myButton.setTitle(myRegularText, for: .normal)

Is there a line of code I can use to remove the attributedTitle on the button so that it will not override the new title I have set for the button? Thanks!

Answer Source

You need to set the AttributedTitle to nil before setting using setTitle for your button, Its working i have checked it.

like this

myButton.setTitle(myRegularText, for: .normal)
myButton.setAttributedTitle(nil, for: .normal)