How can I reverse lookup a localized string - ios

I am interested in finding the localized string key for a given text on the screen.
Is this possible ?
For instance in a contrived example:
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.text = NSLocalizedString(#"The key for this label",nil);
// at runtime
UILabel *theLabel = ... // get the label
NSString *text = theLabel.text;
NSLog(#"%#",text); // prints "Foo bar"
// now I want to get the key:
NSString *localizedKey = ???? NSLocalizedStringReverseLookup(text)
NSLog(#"%#",localizedKey); // should print "The key for this label"

You can do this in some cases as nburk said, but you cannot do it in others:
It is possible that different keys refers to the same string. You cannot find out, which key was the original one.
Strings on screens can contain data pasted into the string via %…. You will not find the string then.
Why do you want to do this?

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];
}

How Receive data from Label, while you have the name of that label in String

I am new in IOS Dev, want to receive text data from one label among many labels, but i have just a name of label in String type variable.
NSString *tag = #"lbl_11";
NSString *recieved_label_data = tag.text;
If the label is stored in a property, you could just use KVC. It can also find ivars in certain situations. Something like this:
UILabel *tagLabel = [self valueForKey:tag];
NSString *recieved_label_data = tagLabel.text;

Displaying Array in Text Label

I'm still sort of new to Xcode, so please be patient with me. Anyway, I'm having a bit of trouble trying to display the whole contents of an array in a UILabel. I'm able to display it by simply using the code
wordList.text = [NSString stringWithFormat:#"List of Words:\n %#", listA];
However upon running, the label ends up displaying a parenthesis and the words on their own lines, as well as quotation marks around the words, and the ending quotation mark and a comma in the line between each word. Example:
List of Words:
(
"apple
",
"banana
",
"etc.
While I do want the words to be displayed in their own lines, I do not want the parenthesis and the closing quotation mark and comma being displayed in a separate line. I would also prefer removing the parenthesis, quotation marks, and commas all together, but I wouldn't mind too much if I'm unable to.
Could anyone please explain why its being displayed as such, and to help me correctly display each word of an array in its own line in a UILabel?
Use this:
NSArray *listOfWords = #[#"One", #"Two", #"Three"];
NSString * stringToDisplay = [listOfWords componentsJoinedByString:#"\n"];
wordList.text = stringToDisplay;
Will Display:
One
Two
Three
The parentheses, quotation marks, and commas are being added because providing an array as an argument to the format specifier %# causes the -(NSString *)description method to be sent to the array. NSArray overrides NSObject's implementation of description and returns a string that represents the contents of the array, formatted as a property list. (As opposed to just returning a string with the array's memory address.) Hence, the extra characters.
You Can use this Code
NSArray *listOfWords = [NSArray arrayWithObjects:
#"one.",
#"two.",
nil];
for (NSString *stringToDisplay in matters)
{
//frame, setting
labelFrame.origin.x = 20.0f;
UILabel *stringToDisplayLabel = [[UILabel alloc] initWithFrame:labelFrame];
stringToDisplayLabel.backgroundColor = [UIColor clearColor];
stringToDisplayLabel.font = [UIFont boldSystemFontOfSize:12.0f];
stringToDisplayLabel.lineBreakMode = NSLineBreakByWordWrapping;
stringToDisplayLabel.numberOfLines = 0;
stringToDisplayLabel.textColor = [UIColor whiteColor];
stringToDisplayLabel.textAlignment = NSTextAlignmentLeft;
//set up text
stringToDisplayLabel.text = stringToDisplay;
//edit frame
[stringToDisplayLabel sizeToFit];
labelFrame.origin.y += stringToDisplayLabel.frame.size.height + 10.0f;
[self.view addSubview:stringToDisplayLabel];
[matterLabel release];
}

Truncate part of text in UILabel

My requirement is that I need to display text in label in such a way that if the length of text is too big to accommodate in one line, i need to truncate it at the end in such a way that only the last few characters(usually a number b/w 1-1000 so text length may vary.) are visible and the text before it is truncated with "...".
So the text will look something like "abcdefgijk...10"
Is there any way I can achieve this?
UILabel *contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(50,100, 150, 30)];
contentLabel.text = #"abcdefghijklmnopqrstuvwxyz10";
contentLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
Add this label to your display. You should get a output something like this
abcdefghijklmnopq...10
Swift 4:
Incase someone runs into this issue like i did,
you need to set your label.numberOfLines = 1
if you have it set to 0 it will truncate at a space.
so your code should look like
label.numberOfLines = 1
label.lineBreakMode = .byTruncatingTail
label.adjustsFontSizeToFitWidth = false
For everyone looking for more recent solution, swift 3 :
yourLabel.lineBreakMode = .byTruncatingMiddle;
Try this:
label.lineBreakMode = NSLineBreakByTruncatingMiddle;
UILineBreakModeMiddleTruncation is deprecated from iOS 6.0.
In Storyboard
Select the Label which you want to truncate the characters.
Choose Attributes Inspector.
Under Label attributes. You can find Line Break
Done.
there are many methods in NSString class use -length and then use any of these
– substringFromIndex:
– substringWithRange:
– substringToIndex:
create a temporary string using NSString stringwithFormat, put your desired charecters you get from substringTo index and "....." then your numbers from string by substringFromIndex.
hope this helps
You can start with finding the length of characters that can be placed in a line, say 'n' characters. You can take help of this link to determine 'n' How to know if NSString fits in UILabel or not and index of the last string which fits?. Next, find the length of the string. If it exceeds n, then extract the last two characters. Ex
NSString * fooString = #"a very long string";
NSString * s2 = [fooString substringWithRange:NSMakeRange([fooString length]-3, 2)];
NSString * s1 = [fooString substringWithRange:NSMakeRange(0 , n-5)];
NSString * newString = [NSString stringWithFormat:#"%#...%#",s1,s2];
We have different Line Break modes for UILabel like
Truncate Head,
Truncate Middle,
Truncate Tail
In Xib you can set the Line Break mode what ever you want
If you using XIB file..
select --> UILable and
select --> Attribute inspector tag and change into Line Breaks-->Truncate tail
simply way to truncate characters...
Here is how to use it, NSLineBreakByTruncatingMiddle
UILabel *temp = [[UILabel alloc]initWithFrame:CGRectMake(5,75, 100, 50)];
[temp setBackgroundColor:[UIColor lightGrayColor]];
temp.lineBreakMode = NSLineBreakByTruncatingMiddle;
temp.text = #"HelloBoss997";
Output :
Hello...s997

Is there an option to implicitly localise labels in Interfacebuilder

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.

Resources