Localization the Navigation Bar Title in swift - 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

Related

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?
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)

Text Field name programatically in swift 4

I've below form:-
I've created this form programatically.
The code for this is below:-
let textFiled = UITextField(frame:CGRectMake(87.0, y, 100.0, 20.0))
textFiled.borderStyle = UITextBorderStyle.Line
textFiled.font = UIFont.systemFontOfSize(8)
Now if I set tag for these text field then it'll store Int value. Which is very tough for me to manipulate this form.
I want to set name like IBOutlet for each text field. So that I can easily handle this form.
Is it possible to do it in Swift 4...
I want Your opinion please....
Similar to tag, we have
accessibilityIdentifier
in which you can add String values
you can just say
textFiled.accessibilityIdentifier = "name"
and get value using the below:-
textFiled.accessibilityIdentifier
UPDATE:
I suggest we never use the accessibilityIdentifier as it's for testing, for your case you should subclass UITextField and add a custom property to identify the text field

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!

Changing app language on the fly in iOS

I am new to iOS and I know this is a very frequently asked question, but I have found no appropriate answer for my query.
I have added text to controls on the app like this
let productDescriptionCell = tableView.dequeueReusableCellWithIdentifier("textDescriptionCell") as! TextDescriptionCell
productDescriptionCell.labelForCell.text = "Description"
productDescriptionCell.labelForCell.sizeToFit()
productDescriptionCell.labelForCell.adjustsFontSizeToFitWidth = true
like the 'Description' text above.
I want to have a button in my app, which lets the user change the language on the fly. I have found the following code as the most relevant but this requires an app restart.
userDefaults.setObject(["\(cc)"], forKey: "AppleLanguages")
userDefaults.synchronize()
In addition, I have text hard coded in my storyboard controllers, I want those localized as well. How can I achieve this?
Thai and English are the languages I need localization for
productTitleCell.textFieldForCell.placeholder = SpecProvider.spec.localizedTuv(createLocalizedString("en", tuvEnglish: "Enter Title", tuvThai: "ป้อนชื่อ"))
func localizedTuv(localizedString: LocalizedString) -> String {
var locale = LocaleSelectionService.sharedInstance.getCachedLocale()
if locale.isEmpty {
locale = ""
}
switch(locale) {
case "en": return localizedString.getTuvEnglish()
case "th": return localizedString.getTuvThai()
default: return localizedString.getTuvEnglish()
}
}
I have tried the above code, but this could get very tedious. Please help, I am really stuck!
You have to create string files, and add the labels for every language you want to support. Once this is done you can use NSLocalizedString for the text used in your ViewController.
Here's a tutorial http://www.raywenderlich.com/64401/internationalization-tutorial-for-ios-2014

Gtk Button inner-border

I add a button to HBox, with expand equal to False, but I want the button to have more spacing between its label and border. I assume it is "inner-border" property, but it is read-only. How can I set it to e.g. 4px?
gtk.Label is a subclass of gtk.Misc which has the method set_padding. If you get the label out of the gtk.Button then you can just call set_padding on it.
You could do something like:
label = gtk.Label("Hello World")
button = gtk.Button()
/* Add 10 pixels border around the label */
label.set_padding(10, 10)
/* Add the label to the button */
button.add(label)
/* Show the label as the button will assume it is already shown */
label.show()
Wrong answer:
What you're looking for is called "padding". When you add your button to the container, for example by calling gtk.Box.pack_start, just set the padding parameter to a positive integer.
Update:
Seems I misread the question. In that case, my guess is that you're supposed to use gtk_widget_modify_style, as inner-border is a style property. You'll first get the style modifier you need by calling gtk_widget_get_modifier_style. You'll then be able to modify the style only for that button using the ressource styles matching rules.
you can use "inner-border" style property of gtk button.
here, small code snippets
In gtkrc file:
style "button_style"
{
GtkButton::inner-border = {10,10,10,10}
}
class "GtkButton" style "button_style"
In .py file:
gtk.rc_parse(rc_file_path + rc_file)
[Edit]
In gtkrc file:
style "button_style"
{
GtkButton::inner-border = {10,10,10,10}
}
widget "*.StyleButton" style "button_style" # apply style for specific name of widget
In .py file:
gtk.rc_parse(rc_file_path + rc_file)
#set name of button
self.style_button.set_name('StyleButton')
hope, it would be helpful.
I sometimes just add spaces in the label !
gtk.Button(" Label ")
to get some spacing.
Hope this could help you.