我相信是懒人推动了世界的发展,既然iphone有了自己的软件盘,我们什么还要自己实现其功能呢。
so,只要寄生在上面就行了。
通过这篇文章给的灵感:
为UIKeyboardTypeNumberPad增加自定义按键
http://www.codeios.com/thread-805-1-1.html
思路:
1.用静态方法找到应用程序当前view(window)中的UIKeyboard的view
2.在键盘的view上帖上自己的view,(精彩了,这个自己的view就是你自己键盘,任意发挥,什么类型键盘都可以做了)
3.根据需要调整系统键盘的大小以满足你想要的尺寸
4.给自己的键盘view上的button添加方法,实现功能
主要代码:
添加自身类为键盘事件的观察者:
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(keyboardWillShow:)
- name:UIKeyboardWillShowNotification
- object:nil];
- - (void)keyboardWillShow:(NSNotification *)note
- {
- UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];//知识点
- for(int i=0; i<[tempWindow.subviews count]; i++)
- {
- keyboard = [tempWindow.subviews objectAtIndex:i];
- if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
- {
- [keyboard setFrame:CGRectMake(0, 460, 320, 345)];
- [self congfigKeypad];
-
- [keyboard addSubview:keyPadView1];
-
- }
- }
- }
- -(void)congfigKeypad
- {
- SearBtn *one = [[SearBtn alloc] initWithFrame:CGRectMake(81, 3, kNumPadW, kNumPadH) index:1 ContextString:@"1" type:kNumPadType];
- [one setImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
- [one addTarget:self action:@selector(buttonClickAtIndex:) forControlEvents:UIControlEventTouchUpInside];
- //......略
- }
- - (void)buttonClickAtIndex:(id)sender
- {
- SearBtn *btnItem = (SearBtn*)sender;
- NSString *str = btnItem->btnText;
- [s_text appendString:str];
- [sBar setText:s_text];
- }
思路:
- if ([s_text length] > 0)
- {
- NSRange rang;
- rang.location = [s_text length] - 1;
- rang.length = 1;
- [s_text deleteCharactersInRange:rang];
- }
继续优化
用textfield的代理方法控制键盘的字串类型,长度,和响应消失: