iPhone开发——监听输入法状态

Home / iOS MrLee 2015-5-29 2917

实现的功能:1)演示监听键盘状态(可解决键盘挡住输入法等问题)2)监听输入法状态 1、新建一SingleView Application,命名为:KeyBoard&InputMethod,工程结果如下:

1356440603_4220

2、修改ViewController.xib如下:

1356440606_1553

3、ViewController.h不作修改,ViewController.m修改后如下:
#import "ViewController.h"  
  
@interface ViewController ()  
  
@end  
  
@implementation ViewController  
  
- (void)viewDidLoad  
{  
    [super viewDidLoad];  
    // Do any additional setup after loading the view, typically from a nib.  
      
    //监听键盘状态  
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];  
    //监听输入法状态  
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeInputMode:) name:UITextInputCurrentInputModeDidChangeNotification object:nil];  
}  
  
#pragma mark Notification  
//keyBoard已经展示出来  
- (void)keyboardDidShow:(NSNotification *)notification  
{  
    NSValue* aValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];  
    CGRect keyboardRect = [aValue CGRectValue];  
    CGRect keyboardFrame = [self.view convertRect:keyboardRect fromView:[[UIApplication sharedApplication] keyWindow]];  
    CGFloat keyboardHeight = keyboardFrame.size.height;  
    NSLog(@"##keboardHeight=%.2f",keyboardHeight);  
}  
  
//输入法发生切换  
-(void)changeInputMode:(NSNotification *)notification{  
    NSString *inputMethod = [[UITextInputMode currentInputMode] primaryLanguage];  
    NSLog(@"inputMethod=%@",inputMethod);  
}  
  
  
- (void)didReceiveMemoryWarning  
{  
    [super didReceiveMemoryWarning];  
    // Dispose of any resources that can be recreated.  
}  
  
@end

本文链接:https://www.it72.com/3148.htm

推荐阅读
最新回复 (0)
返回