How to use UITextInputMode - iphone

I need create a method which detects keyboard input language . for example when keyboard language is French does something and when is English does something else
I search on the Internet and found UITextInputMode but I don't know how to use it , I would be grateful if you help me . thanks

It's quite simple, you can do it this way:
UITextInputMode *textInput = [UITextInputMode currentInputMode];
NSString *primaryLanguage = textInput.primaryLanguage;
NSLog(#"Current text input is: %#", primaryLanguage);
As noted in Apple docs, "The value of this property is a BCP 47 language code such as “es”, “en-US”, or “fr-CA”".
If you need to be notified about changes, you add your controller as observer for UITextInputCurrentInputModeDidChangeNotification

Related

Get writing language of "uitextview" iphone?

How can i get writing language of uitextview?
or
how can i know uitextview is right to left or left to right in current state?
[textView baseWritingDirectionForPosition:[textView beginningOfDocument] inDirection:UITextStorageDirectionForward] == UITextWritingDirectionRightToLeft
Should return true if the UITextView is right-to-left, and false otherwise.
Further documentation can be found here, if you need it!
In answer to your other question, you can get the keyboard language used from UITextInputMode docs: http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextInputMode_Class/Reference/Reference.html
This solution worked better for me
[NSLocale characterDirectionForLanguage:self.textView.textInputMode.primaryLanguage] == NSLocaleLanguageDirectionRightToLeft

Real Time Search in UITableView

I have implemented a UISearchBar for finding an element in UITableView. Everything seems to work fine, and now I am at the part where I need to actually perform real-time search for every key pressed in the textField, and narrow down the search with every button press.
So before I start coding, I wanted to know if there is any inbuilt library function that could help me with live search ? (string comparison) or anything ?
Your answer could save me some time.
Thanks.
It so happens that there is in fact an inbuilt function that helps with RealTime Search. Phew !
NSRange match = [userNameString rangeOfString:searchText options:NSCaseInsensitiveSearch];
// match.location will provide the exact location of a match of searchText with a String
// match.length will provide the length of match

Problems with escaped characters in JSON string

I have a JSON-string where I know where the problem is, I just can't figure out what to do. I have looked up the "forbidden characters" in a JSON-string but it just doesn't work.
When you run the show-method for FBStreamDialog for iPhone a view comes up with how it's going to look like when it's finally posted on the wall.
This happends when the "description"-property in my JSON-string is hard coded like #"Testing". But as soon as I add the text fetched from a data source which looks like this, it doesn't work:
"description":"
LIVE: Uk's No:1 Reggae Singer Bitty Mclean + Joey Fever, Sthlms No:1 Reggae Voice.
DJs
Deejay Flash & Micke Goulos + Mc Fabulous G.
The Vinyl Bar
Up...
"
Note: I only show the "description"-property of the JSON-string, because there is where the problem is.
So what I tried to do was, as I've explained before, to add the string "Testing" in the "description"-property. This worked. But I wanted to have the data source property of "description", of course. So I tried to replace all the characters that isn't a letter with this code:
shortString = [shortString stringByReplacingOccurrencesOfString:#"&" withString:#"och"];
shortString = [shortString stringByReplacingOccurrencesOfString:#"+" withString:#"plus"];
shortString = [shortString stringByReplacingOccurrencesOfString:#"," withString:#"komma"];
shortString = [shortString stringByReplacingOccurrencesOfString:#"'" withString:#"apostrof"];
shortString = [shortString stringByReplacingOccurrencesOfString:#":" withString:#"colon"];
The output of that is:
"description":"LIVEcolon Ukapostrofs Nocolon1 Reggae Singer Bitty Mclean plus Joey Feverkomma Sthlms Nocolon1 Reggae Voice.
DJs
Deejay Flash och Micke Goulos plus Mc Fabulous G.
The Vinyl Bar
Up...
Which looks like a approvable JSON string?
But apparently not, because the facebook view never shows how it's going to look if I use the data source "description"-property. It just shows the text box "What's on your mind".
This is driving me crazy.
Finally!
I understand why you didn't answer this question. How could you know that facebook connect doesn't allow \n in their StreamDialog's. Not for iPhone anyway.
So the solution was to replace \n with a whitespace or whatever you want.

iPhone: Reading Text From File and UISegmentedControl

First off, I'm a complete beginner.
That said, I thought an ambitious longer-term project/learning experience would be to create an app that displayed daily quotes, like those cheesy day-by-day calendars our grandmothers have in their bathrooms. I want it to have two per day, each one represented by a tab in a UISegmentedControl. That's the long term. Right now I'd be happy with getting a single day's worth of quotes functioning.
Onto the questions:
How can I get text saved in a .txt or .rtf file to be displayed in a UITextView? Preferably without using 'stringWithContentsOfFile,' since Xcode is telling me that's deprecated.
How can I get content from a different file (or maybe a different portion of the same file...?) to be displayed when the user taps the second segment?
If I can get it running so that those two conditions are met and I understand what's going on, I'll consider the day a success. Thanks!
1.
NSError *error = nil;
NSStringEncoding stringEncoding;
NSString *fileText = [NSString stringWithContentsOfFile:#"/path" usedEncoding:&stringEncoding error:&error];
myTextView.text = fileText;
The error and encoding are optional, and you can pass in nil for both. But if you care about the error, or what encoding the file was in they will have useful info in them after the string is created.
2.
Set the valueChanged outlet in Interface Builder to an IBAction on your controller, such as setSegmentValue:. Then, assuming you have an array of quote strings:
- (IBAction)setSegmentValue:(id)sender {
UISegmentedControl *control = (UISegmentedControl*)sender;
NSString *quote = [quotes objectAtIndex:control.selectedSegmentIndex];
myTextView.text = quote;
}
Even though stringWithContentsOfFile: is deprecated, stringWithContentsOfFile:usedEncoding:error: is not. That is the standard method to use for reading from files.
As for the second question, you simply test the state of the segmented control and perform as action based on it. Admittedly this is a high level answer but should get you going.

iPhone en_* sublanguage localization

I want to localize strings in my iphone app for en_GB and other 'en' sub-languages, but XCode and the iphone refuse to let this happen. I have created a localization of "Localizable.strings" for en_GB and en_US (I tried both hyphens and underscores) for testing purposes, but they just aren't recognized. The only language code that works is simply "en" (displayed as "English" in XCode).
I can't believe this isn't possible, so what am I doing wrong? I'm also hoping to get the typical 'cascading' behaviour where if a string isn't found in the sub-language e.g. "en_GB" then it should be taken from "en" if possible. Help?
When you choose 'English' from the list of languages on the iPhone preferences, that actually means the 'en_US' language.
So until apple update their software with additional sublanguages like "English (British)" etc. we are left with going by the locale region setting, and loading strings manually from another string table.
However, the language and regional locale are separated for a reason: a Spanish user in the UK may want dates/times formatted according to the local customs, but program strings in their native tongue. It would be incorrect to detect the regional locale (UK) and therefore display UK strings.
So basically there is no way to do this currently.
What you're doing should work according to the docs. But it appears that the iPhoneOS implementation is at odds with the documentation. According to Radar 6158876, there's no support for en_GB language, only locale (date formats and the like).
I found the same problem.
BTW, if you look at the iPhone Settings -> General -> International menu, it makes the distinction between language and region quite clear:
Languages:
-English
Region Format:
-United States
-United Kingdom
The localization framework only appears to pay attention to the language, not the region.
I'm tempted to raise an enhancement request for this with Apple, as IMO it is reasonable that a user might want to use British English (for the text) whilst being in the United States (where, say, phone numbers should be in US format).
This can actually be done - check my solution here - iPhone App Localization - English problems?
Create a separate string resource, say UKLocalization.strings, and create localizations for each of your supported languages. For all localizations other than en this file is empty. For en, it contains only the strings that have unique en_GB spelling.
Next, you create a replacement for NSLocalizationString that will first check the UKLocalization table before falling back to the standard localization table.
e.g.:
static NSString* _locTable = nil;
void RTLocalizationInit()
{
_locTable = nil;
NSString* country = [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode];
if ([country isEqual:#"GB"])
{
_locTable = #"UKLocalization";
}
}
NSString* RTLocalizedString(NSString* key, NSString* ignored)
{
NSString* value = nil;
value = [[NSBundle mainBundle] localizedStringForKey:key value:nil table: _locTable];
if (value == key)
{
value = NSLocalizedString(key, #"");
}
return value;
}
I’m not sure in which version of iOS it was introduced, but iOS 7 definitely has a ‘British English’ language preference that will pick up resources from the en_GB.lproj directory. The various hacks floating around the web shouldn’t be necessary unless you’re after a more specialised* dialect.
*see what I did there ;)