navsegda
navsegda

Reputation: 1

Localize button Swift

For example I've got an app which has a textLabel and a button which are firstly set as:

mainLabel.attributedText = "labelNewText".uppercased()   
mainButton.titleLabel?.attributedText = "buttonNewText".uppercased()

Then I've created a Localization file, where I set a some values for a German language:

"labelNewText" = "Etikette";
"buttonNewText" = "Taste";

And rewrote set ups for label and button like:

mainLabel.attributedText = "\(NSLocalizedString("labelNewText", comment: ""))".uppercased()
mainButton.titleLabel?.attributedText = "\(NSLocalizedString("buttonNewText", comment: ""))".uppercased()

Though, right after after I change my phone language setting to German, the translation only works for a Label but is not working for button. What am I doing wrong and how to localise button titleLabel?

Upvotes: 0

Views: 1102

Answers (1)

Eilon
Eilon

Reputation: 2982

You have to use UIButton's setAttributedTitle:forState: instead of trying to manipulate the text label itself. So for example:

mainButton.setAttributedTitle(myAttributeString, forState: .normal)

Upvotes: 2

Related Questions