Localize button Swift - 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?

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)

Related

Button text shrinks when the text is changed

Ones I change the text in button it's getting stink to font size of ~18 even though initial font size is 55. Button is not custom. It has System font.
button.setTitle("Some button text", for: .normal)
I tried manually change font size in code but it doesn't help
button.titleLabel?.font = UIFont.systemFont(ofSize: 55)
I also tried to change the name through configuration function, but there is no font size and it shrinks anyway.
`
button.configuration = paperButtonConfiguration()
func buttonConfiguration() -> UIButton.Configuration {
var config: UIButton.Configuration = .plain()
config.title = "Some button text"
//Should probably be something like this but it doesn't work
//config.font = UIFont.systemFont(ofSize: 55)
return config
}
`
While I was writing this question I kind of solve this, but not completely. If I change the button style from "plain" to "default" it works, but some weird animation of fading and appearance occurs.
How could I do this with "plain"?
UIButton has four type are Plain,Grain,Tinted,Filled .When you create a button in storyboard , button type automatically set with Plain that means new UIButton configurations is on. If you want to old behaviour , you must set style plain to default.
Or , If you want one of the style above . You need to set font like
button.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { incoming in
var outgoing = incoming
outgoing.font = UIFont.systemFont(ofSize: 50)
return outgoing
}

Forcing UIButton to have just one title line

I'm trying to force the UIButton to accept just one line of text, and if the title is too long I would like to have "..." at the end or in the middle of the title. but when I try out the code below, unfortunately, it doesn't work, it still gives multiline title text.
titleButton.titleLabel!.lineBreakMode = .byWordWrapping
titleButton.titleLabel!.numberOfLines = 1
titleButton.titleLabel!.textAlignment = .center
The main problem was the button style which was on the "plain", the magic happened after changing it to default!
It sounds like you want lineBreakMode.byTruncatingTail or .byTruncatingMiddle.

NSSearchField - How to select all text? (Swift/Storyboards on macOS)

It seems everything online is mostly about iOS/UI controls, not macOS/Cocoa NS controls. Anyway, How does one make an NSSearchField select all text in the field programatically? I have tried multiple methods adapted from the iOS UISearchBar implementations, but none of them compiled or worked. I just want to, for instance, press a button and have it hilight the text that is inside the NSSearchField's text field. I can't seem to find a method within it that allows this to happen.
Thank you for your help/consideration!
All editing in NSTextField is handled by NSText (which inherits from NSTextView). So, to select the text in your search field, you need to set the selected range in field's current editor. This example highlights the whole text.
Objective-C:
NSRange range = NSMakeRange(0, searchField.stringValue.length);
NSText *editor = [searchField currentEditor];
[editor setSelectedRange:range];
Swift
let range = NSRange(location: 0, length: searchField.stringValue.length)
let editor = searchField.currentEditor()
editor?.selectedRange = range

Localization the Navigation Bar Title in swift

I write navigation bar title in Attributes inspector. I would like to translate that title when I switch to other language.
self.navigationItem.title = NSLocalizedString("Login", comment: "")
I write it in override func viewDidLoad().
I took navigation item Object ID "S3Z-Mr-Qda" and translate it in Main.strings file.
/* Class = "UINavigationItem"; text = "Package History"; ObjectID = "S3Z-Mr-Qda"; */
"S3Z-Mr-Qda.text" = "Login";
However, it does not change at all and how to implement it in swift?
You can actually provide a translation to the title using that method.
The only thing that you did wrong was setting the property "text". As you can see in the code version, a navigationItem has a title property instead of text.
This would work:
"S3Z-Mr-Qda.title" = "Login";
I think you should put your translation inside a generic
Localizable.strings file instead of the storyboard translation file.
Then use your same code to load the title text:
self.navigationItem.title = NSLocalizedString("Login", comment: "")
Take a look at the official doc in order to create this kind of file:
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/LoadingResources/Strings/Strings.html

Add Placeholder to UITextField, how to set the placeholder text programmatically in swift?

I'm pulling out a phone number from a database, and when the user begins editing in the text field to change that phone number I'd like to use the number I currently have in the database as the placeholder. Since this information changes with each user, how can I set it programmatically in swift?
You need to get the phone number from your database first (convert them to String), then you set placeholder of your textField to that String, like so
textField.placeholder = phoneNumberString
Swift 3
If your textField has text, you need to first set text property to nil, then set placeholder text:
textField.text = nil
textField.placeholder = "My Placeholder Text"
Important to note for anyone else reading this, setting placeholder text in the main.storyboard seems to nullify this solution, so I had to first clear out my placeholders in the storyboard before implementing this. Once that was done #Khuong and #Himanshu's answer worked perfectly.
Apply this line of code in to View Did Load
new_Password.attributedPlaceholder =
NSAttributedString(string: " New Password", attributes: [NSForegroundColorAttributeName : UIColor.white]) // new_Password : our text feild name
Fetch your desired data from your database (Core data) and after converting it into string format... say phoneString
use this line to set this string as a placeholder text
phoneTextField.placeholder = phoneString
Objective-C code:
[usernameText setPlaceholder:#"My Placeholder Text"];
Just a note to say if you have changed your textfield's text and background colors programmatically, you can't do that with the placeholder text's colors and must set up an Attributed Placeholder instead. This is a problem if your device is in put in "dark mode" and are trying to make a non-dark mode screen by hand - you might not be able to see the placeholder!