UITextInputMode类的使用方法

UITextInputMode大家看了是不是有些陌生呢?这个类是在4.2之后才有的一个新的类,是用来获取当前文本输入模式的。这个可能说的有些模糊。说白了就是在用户输入文本时,判断用户使用的是什么键盘的。

其实用法很简单哦。

如果要在用户改变输入方式时,获得此值,可如此使用:

首先在用户开始输入之前注册通知:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeMode:) name:@"UITextInputCurrentInputModeDidChangeNotification" object:nil];

然后实现上面的方法:

-(void) changeMode:(NSNotification *)notification{

NSLog(@"%@",[[UITextInputMode currentInputMode] primaryLanguage]);

}

这样就能拿到值了。

下面是LOG结果:

2011-07-18 14:32:48.565 UIFont[2447:207] zh-Hans //简体汉字拼音

2011-07-18 14:32:50.784 UIFont[2447:207] en-US   //英文

2011-07-18 14:32:51.344 UIFont[2447:207] zh-Hans //简体手写

2011-07-18 14:32:51.807 UIFont[2447:207] zh-Hans //简体笔画

2011-07-18 14:32:53.271 UIFont[2447:207] zh-Hant //繁体手写

2011-07-18 14:32:54.062 UIFont[2447:207] zh-Hant //繁体仓颉

2011-07-18 14:32:54.822 UIFont[2447:207] zh-Hant //繁体笔画

 

- (void)viewDidLoad

{

    [super viewDidLoad];

    NSNotificationCenter *nCenter = [NSNotificationCenter defaultCenter];

    [nCenter addObserver:self selector:@selector(languageChanged:) name:UITextInputCurrentInputModeDidChangeNotification object:nil];

    [self languageChanged:nil];

}

-(void)languageChanged:(NSNotification*)notification

{

    for(UITextInputMode *mode in [UITextInputMode activeInputModes])

    {

        NSLog(@"Input mode: %@", mode);

        NSLog(@"Input language: %@", mode.primaryLanguage);

    }

    NSLog(@"Notification: %@", notification);

    UITextInputMode *current = [UITextInputMode currentInputMode];

    NSLog(@"Current: %@", current.primaryLanguage);

}

原文地址:https://www.cnblogs.com/ChouDanDan/p/5051799.html