我在視圖控制器上有一個uitextfield對象。用戶可以使用任何鍵盤語言輸入任何文本。如何獲取uitextfield的鍵盤語言環境
問題是:如何通過用戶選擇的鍵盤獲取uitextfield內的輸入文本的語言ID?
我在視圖控制器上有一個uitextfield對象。用戶可以使用任何鍵盤語言輸入任何文本。如何獲取uitextfield的鍵盤語言環境
問題是:如何通過用戶選擇的鍵盤獲取uitextfield內的輸入文本的語言ID?
以上iOS7.0
- (void)textFieldDidEndEditing:(UITextField *)textField {
NSString *primaryLanguageId = textField.textInputMode.primaryLanguage;
}
iOS7.0及以下
- (void)textFieldDidEndEditing:(UITextField *)textField {
NSString *primaryLanguageId = [UITextInputMode currentInputMode].primaryLanguage;
}
只要您在使用[UITextInputMode currentInputMode].primaryLanguage
獲得的launguage代碼字符串,但也必須添加觀察者,因爲如果鍵盤launguage是然後這個觀察者通知語言改變。
Objective-C的:
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(changeInputMode:)
name:UITextInputCurrentInputModeDidChangeNotification object:nil];}
}
//This method prints the currently selected input language (like "en_US" or "de_DE"):
-(void)changeInputMode:(NSNotification *)notification
{
NSString *inputMethod = [[UITextInputMode currentInputMode] primaryLanguage];
NSLog(@"inputMethod=%@",inputMethod);
}
夫特:
class ViewController: UIViewController
{
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "changeInputMode:",
name: UITextInputCurrentInputModeDidChangeNotification, object: nil)
}
func changeInputMode(notification : NSNotification)
{
let inputMethod = UITextInputMode.currentInputMode().primaryLanguage
println("inputMethod: \(inputMethod)")
}
}