Is there an option to implicitly localise labels in Interfacebuilder - ios

Somewhere in a blog post I stumbled upon a strings file which looked like this:
// de.lproj/Localizable.strings
"This is the title" = "Das ist der Titel"
To me this looked like the actual labels in Interface builder were processed by the compiler so that no explicit translations using NSLocalizedString(#"SOME_IDENTIFIER", #""); would be necessary any more.
My question now, is whether there is some kind of shortcut or do I need to localise all my individual labels on my view e.g. in the awakeFromNib method.

I have figured out a way to semi-automate the process so that I don't have to do this:
label1.text = NSLocalizedString(#"label1_key", #"");
label2.text = NSLocalizedString(#"label2_key", #"");
....
labeln.text = NSLocalizedString(#"labeln_key", #"");
So for all labels which should be localised I set their text to __KeyForLabelX in IB. Then in the viewWillAppear method of the viewcontroller I loop through the items on the view and set the text to the localized value:
for (UIView *view in self.view){
if([view isMemberOfClass:[UILabel class]]){
UILabel *l = (UILabel *)view;
BOOL shouldTranslate = [l.text rangeOfString:#"__"].location != NSNotFound;
NSString *key = [l.text stringByReplacingOccurrencesOfString:#"__" withString:#"TranslationPrefix"];
if (shouldTranslate){
l.text = NSLocalizedString(key, #"");
}
}
}
My .strings file then look like this:
"TranslationPrefixKeyForLabelX" = "Translation of Label X";
Update: To further adapt the mechanism you could also check for other UIViews like UIButtons, UITextFields (including prompt text) etc.

Related

How to create a custom software key UITextField/NSTextField

I want to create a custom NSTextField/UITextField like this for entering a software key (pardon my paint skills).
Does anybody have any suggestions as to how I should go about this?
My lazy solution would be to give it a placeholder string with spaces and dashes in between, and as they type just mask those dashes into their string. But I wanted to see if anybody else had some input-- or if I should just go with your standard separate text fields
-(void)viewDidLoad
{
UITextField *firstPart = [[UITextField alloc] initWithFrame: YOUR_FRAME];
firstPart.placeholder = #"-";
firstPart.textAlignment = NSTextAlignmentCenter;
[self.view addSubview: firstPart];
// Create others
}
-(IBAction)unlockBtnPressed:(id)sender
{
NSString *softwareKey = [NSString stringWithFormat: #"%# - %# - %#", firstPart.text, secondPart.text, thirdPart.text];
}

Adding label text together

Hi im relatively new to iOS and I'm just wondering how i would add two labels together.
- (IBAction)switch1
{
if (switch1.on) {
value1.text = #"3";
} else {
value1.text = #"0";
}
}
- (IBAction)switch2
{
if (switch2.on) {
value2.text = #"3";
} else {
value2.text = #"0";
}
}
As you can see i have used two switches which would show two different values if they were turned on or off.
Could someone help me understand how i would add two values together.
I.e if switch one was on and switch two was off the value would be three i want this value to be shown in another label.
So far i have come up with this but for some reason it doesn't work, i have a feeling it is the format specifier but I'm not sure.
int sum = [[value1 text] intValue] + [[value2 text] intValue];
value3.text = [NSString stringWithFormat:#"%d", sum];
Dont you have this:
int sum = [[value1 text] intValue] + [[value2 text] intValue];
value3.text = [NSString stringWithFormat:#"%d", sum];
in ViewDidLoad or something? Because you have to call this at the end of both IBActions. If you don't, your final value will never change.
Make sure you properly created an outlet connection in Interface Builder as described here:
Creating an Outlet Connection
In short words. Ctrl+Click & Drag from the UISwitch to File’s Owner and click the new switch1 or switch2 action. Create outlets for the text fields and switches and link them.
Set breakpoints in switch1 and switch2 methods and ensure there are being called.
Use po command in the console to check if the text fields and switches are configured correctly.
For example:
po _textField1
should print text field's description. It will print nil when the text field is not there - not linked to a control in interface builder.

Is there a way to implicitly make an button be multi-line?

I want a button that is similar to this:
ONE
TWO
Where one word is over the other. However, it may be:
THREE
FOUR
Or any number, really. Is there a way with NSAttributedStrings that I can say to always have a line-break after the first word?
It's really not about NSAttributedString or NSString, but rather about the button itself. You may think of subclassing UIButton and overriding setTitle:forState: to automatically replace the first space with a \n.
Specifically the setTitle:forState: would look something like this
- (void)setTitle:(NSString *)title forState:(UIControlState)state {
NSRange firstSpaceRanger = [title rangeOfString:#" "];
if (firstSpaceRanger.location != NSNotFound) {
title = [title stringByReplacingCharactersInRange:firstSpaceRanger withString:#"\n"];
}
[super setTitle:title forState:state];
}
For instance, given one two three this will produce
one
two three
In order to make the button multiline you can do, in the UIButton initializer:
self.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
Yes, firstSpaceRanger is intentional. I couldn't resist.

UISegmentedControl and localization

I have a .xib file, with accompanying .strings files for different languages. The .xib file contains a label, and a UISegmentedControl.
When asking IB to localize the .xib file, I get the following .strings file:
"6.segmentTitles[0]" = "title1";
// ...More strings related to the segmented control...
"11.text" = "bla";
The 'bla' string belongs to the label.
Changing the 'bla` string is reflected in runtime, while changing the 'title1' string does not. Anyone knows why?
This question is not new, but as it is still without any answers I will add my solution as it may help others.
Generally, as it's mentioned above, it is an open bug that UISegmentedControl segment titles do not pick up localization strings.
- (void)viewDidLoad
{
...
// Locale will be picked automatically by NSBundle.
NSString *resourcePath =[[NSBundle mainBundle] pathForResource:#"MainStoryboard" ofType:#"strings"];
NSDictionary *resourceDict = [NSDictionary dictionaryWithContentsOfFile:resourcePath];
[self.segmentedControl setTitle:[resourceDict objectForKey:#"COo-BO-Ryl.segmentTitles[0]"] forSegmentAtIndex:0];
[self.segmentedControl setTitle:[resourceDict objectForKey:#"COo-BO-Ryl.segmentTitles[1]"] forSegmentAtIndex:1];
}
Where COo-BO-Ryl is the Object ID of segmentedControl.
Not very pretty, but does the job.

Newbie xcode: UILabel is null but it's not

I was going through a tutorial that seemed quite straightforward but could not get it to work. The goal is to have a label and a button in the UI view that are created with IB. The label is supposed to say "Hello World" and when you click the button it will change the text to "Hello iPhone". When I implement the code, the label doesn't change. when I debug, I found out the label is "null" but I can't figure out why... I would really appreciate your help with this.
The label has a tag value "55".
My code looks like this:
in the interface file:
-(IBAction)buttonTapped;
in the main file:
-(IBAction)buttonTapped{
UILabel *label = (UILabel*) [window viewWithTag:55];
NSLog(#"The label's text is %s",label.text); //my debug statement
if([label.text isEqualToString:#"Hello World"])
label.text = #"Hello iPhone";
else
label.text = #"Hello World";
}
Assuming you're in a ViewController. I don't know what your window property is. Also you should be using "%#" for strings. Format Specifiers
-(IBAction) buttonTapped:(UIButton*)sender{
UILabel *label = (UILabel*) [self.view viewWithTag:55];
NSLog(#"The label's text is %#",label.text); //my debug statement
if([label.text isEqualToString:#"Hello World"])
label.text = #"Hello iPhone";
else
label.text = #"Hello World";
}
Reading between the lines it sounds like your action method is not being fired.
You weren't explicit in saying how you knew the label text was blank, but can we assume that you did actually get the NSLog to output its results? If you didn't then I refer to my first sentence... Check that you have wired up your Button correctly in the Interface Builder, to point to an outlet action.
in your #interface you should have:
IBOutlet UILabel *label;
then in your -(IBAction)buttonTapped just:
NSLog(#"The label's text is %s",label.text); //my debug statement
if([label.text isEqualToString:#"Hello World"])
label.text = #"Hello iPhone";
else
label.text = #"Hello World";
}

Resources