new2ios
new2ios

Reputation: 1350

How to set underline text of UIButton (which has custom localisation in Xcode 6.3)?

I know that this question is asked many times, but the most answers are several years ago. I hope there there is a easiest solution for Xcode 6.3.

I have UIViewController in which I have 2 buttons which must be underlined. I also use custom solution for translating resources (my language is set inside the App - according the specification).

I see that I can set attributed text in Xcode 6.3, but in that case the text in the button (resource) is not translated.

I tried to set NSMutableAttributedString as text, but the setTitle method needs NSString and I have error.

I see that there are answers with making custom button type, but I think this is a bit complex (I have those two buttons only in the UIViewController).

Is there any other solution?

Upvotes: 1

Views: 932

Answers (1)

matt
matt

Reputation: 536037

I tried to set NSMutableAttributedString as text, but the setTitle method needs NSString and I have error.

Because you are calling the wrong method. You want the setAttributedTitle:forState: method, which solves that problem.

I see that I can set attributed text in Xcode 6.3, but in that case the text in the button (resource) is not translated.

Set the attributed text in code, by calling setAttributedTitle:forState:, starting with a string that is defined in code. When you define that string, call NSLocalizedString. Define the localized string in a Localizable.strings file. Now the text is translated.

(And indeed in Xcode 6, localization is easier than ever, because all your localized material can be exported / imported as a unified .xliff file.)

Upvotes: 1

Related Questions