Setting the iPhone keyboard language

前端 未结 7 1942
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-27 20:15

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

相关标签:
7条回答
  • 2020-11-27 20:45

    Not sure why you'd want to do this -- I don't speak Russian, and I'd be a little alarmed if all of the sudden a Russian keyboard appeared.

    The user can choose which language they want in their iPhone Settings app. That then determines which keyboard appears when they tap a text field.

    0 讨论(0)
  • 2020-11-27 20:48

    This is a user defined setting and can not be messed with by the programmer, unfortunately.

    0 讨论(0)
  • 2020-11-27 20:51

    I want to do this because I am writing a language learning application.

    What makes the current keyboard selection setup on the iPhone even worse (at least for me) is that it doesn't remember the keyboard for each appliaction. So that if you change it for one app it remains the same keyboard for the next app you open.

    0 讨论(0)
  • 2020-11-27 21:02

    I don't know about iPhone 3.0 SDK, but as of 2.x, you can't control this. What you can do, however, is make a custom keyboard. It's a lot more work, but you could remake the layouts for each language.

    0 讨论(0)
  • 2020-11-27 21:03

    In answer to the the question about English keyboard - Apple did design for that - you can use the ASCII keyboard - which you can choose with: UIKeyboardTypeASCIICapable.

    0 讨论(0)
  • 2020-11-27 21:04

    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 <Foundation/Foundation.h>
    #import <AudioToolbox/AudioToolbox.h>
    
    @interface KoreanOnlyInput : NSObject <UITextFieldDelegate>
    {
        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
    
    0 讨论(0)
提交回复
热议问题