RanLearns
RanLearns

Reputation: 4176

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!

Upvotes: 3

Views: 2074

Answers (2)

bola gamal
bola gamal

Reputation: 109

If you use the underline attribute, you should to set the AttributedTitle by this method like this:

myButton.setAttributedTitle(NSAttributedString(string: myRegularText, attributes: nil), for: .normal)

Upvotes: 1

Rajat
Rajat

Reputation: 11127

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

like this

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

Upvotes: 5

Related Questions