I\'d like the iPhone virtual keyboard to appear pre-set to a particular language (Russian for example) when the user taps a UITextField. Is there a way to do this in Cocoa c
You can't set the keyboard to your desired character set, but the next best thing is to prevent the user entering characters from any other character set. The delegate below does this and also buzzes the iPhone's vibrator when incorrect characters are entered, which works well to quickly alert the user that they have the wrong keyboard selected. This is for Korean but easily modified for other languages (see comments):
Header file:
#import
#import
@interface KoreanOnlyInput : NSObject
{
NSMutableCharacterSet* koreanUnicode;
}
@end
.m file:
#import "KoreanOnlyInput.h"
@implementation KoreanOnlyInput
- (id)init
{
self = [super init];
if (self) {
// From http://www.unicodemap.org/ :
// 0x1100 - 0x11FF : Hangul Jamo (256)
// 0x3130 - 0x318F : Hangul Compatibility Jamo (96)
// 0xAC00 - 0xD7A3 : Hangul Syllables (11172)
koreanUnicode = [[NSMutableCharacterSet alloc] init];
NSRange range;
range.location = 0x1100;
range.length = 1 + 0x11FF - range.location;
[koreanUnicode addCharactersInRange:range];
range.location = 0x3130;
range.length = 1 + 0x318F - range.location;
[koreanUnicode addCharactersInRange:range];
range.location = 0xAC00;
range.length = 1 + 0xD7A3 - range.location;
[koreanUnicode addCharactersInRange:range];
}
return self;
}
- (void)dealloc
{
[koreanUnicode release];
[super dealloc];
}
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string
{
if ([string isEqualToString:@"\n"])
return YES;
BOOL shouldChange = YES;
for (int i=0; i<[string length]; i++)
{
if (![koreanUnicode characterIsMember:[string characterAtIndex:i]])
shouldChange = NO;
}
if (!shouldChange)
{
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
return shouldChange;
}
@end