一、起因
iPhone 的键盘,特别是数字键盘,往往不能满足程序的输入需要。最典型的例子就是在数字键盘上添加一个“.”,用来输入小数点。安装 iPhone SDK 官方的观点,如果要使用小数点键盘,那只好使用数字和符号键盘,但那样没个按键很小,且不需要的按键太多。
二、现有方案
针对这种情况,最早的解决方案,请参考这里:http://www.cnblogs.com/mac_arthur/archive/2010/05/18/1738363.html。使用的是 以下代码来检测一个 Notification。
引用
[[N
snotificationCenter defaultCenter] addObserver:self
SELEctor:@
SELEctor(addCustomKeyBoardButton)
NAME:UIKeyboardWillShowNotification
ob
ject:nil];
这种
方法,在 iOS 4.0 后失效了,原因有两个,一是UIKeyboard
WillShowNotification的时候,
键盘根本没有创建出来;另外,Class Name 也被外包了一层,叫做UIPeripheralHostView。
于是又有了一种改进的方案,请看这里:
http://www.neoos.ch/news/46-development/54-uikeyboardtypenumberpad-and-the-missing-return-key。主要的改进是在UIKeyboard
DidShowNotification的
通知消息中来
修改键盘。但正如作者所说,这里有个不完美的情况,只能等
键盘动画
显示完成之后,才能
添加显示我们的东西,视觉
效果不好。
三、改进方案
经过试验,找到
一个比较完美的方案:在 UITextField 的becomeFirstResponder和resignFirstResponder中
修改键盘。
定以
一个类,假如叫做 KBCustomTextField : UITextField,
在这个类中加入一下
代码:
//
- (BOOL)becomeFirstResponder
{
BOOL ret = [super becomeFirstResponder];
[self modifyKeyView:@"
numberPad-Empty" display:@"." represent:@"." interaction:@"
String"];
return ret;
}
//
- (BOOL)resignFirstResponder
{
BOOL ret = [super resignFirstResponder];
[self modifyKeyView:@"
numberPad-Empty" display:nil represent:nil interaction:@"None"];
return ret;
}
modifyKeyView 的实现
可以参看附件。我使用的是循环查找UIKBKeyView类,这是Apple 的 Private API(
私有API的声明可以
在这里找:
http://github.com/kennytm/iphone-private-frameworks/tree/master/UIKit/),不确信能否通过 App Store 的审核:)
更近一步地,我完善了一下 KBCustomTextField,通过这个类,非常方
便地就可以做到
自定义输入
键盘:
下载
文件 (已下载 51 次)
1. 使用小数点和数字
键盘:非常简单,只要在 IB 中把 UITextField 的类改成KBCustomTextField,就OK了,不用一行
代码,
效果如下图:
2. 在
键盘的按键上
添加一个文字按钮(并指定处理动作):设置KBCustomTextField.kbDelegate,实现这两个
函数即可:
// [Yonsm] Handle keyboard show
- (void)keyboardShow:(KBCustomTextField *)sender
{
[sender addCustomButton:@"
numberPad-Empty"
title:@"DONE" target:self action:@
SELEctor(onButton:)];
}
// [Yonsm] Handle keyboard hide
- (void)keyboardHide:(KBCustomTextField *)sender
{
[sender delCustomButton:@"
numberPad-Empty"];
}
3. 更近一步地,这两个 Delegate
函数中,你只要通过 name 来查找到想要
修改的 UIKBKeyView,就可以随
便修改它。name 可以通过 KBCustomTextField 的#define _LOG_KEY_VIEW来列出所有的按键
名称。name 为 nil 则找任何的 UIKBKeyView(可以用他的 .superview 来找到整个
键盘View,做更多的处理)。
下面是 iPhone 数字
键盘的前面10个按键(后面忘了打出来了:)
Found View: UIPeripheralHostView
Found View: UIKeyboardAutomatic
Found View: UIKeyboardImpl
Found View: UIKeyboardLayoutStar
Found View: UIKBKeyplaneView
Found View: UIKBKeyView
NAME=
numberPad-1
represented
String=1 display
String=1 displayType=
numberPad interactionType=
String displayRowHint=Row1
Found View: UIKBKeyView
NAME=
numberPad-2
represented
String=2 display
String=2/ABC displayType=
numberPad interactionType=
String displayRowHint=Row1
Found View: UIKBKeyView
NAME=
numberPad-3
represented
String=3 display
String=3/DEF displayType=
numberPad interactionType=
String displayRowHint=Row1
Found View: UIKBKeyView
NAME=
numberPad-4
represented
String=4 display
String=4/GHI displayType=
numberPad interactionType=
String displayRowHint=Row2
Found View: UIKBKeyView
NAME=
numberPad-6
represented
String=6 display
String=6/MNO displayType=
numberPad interactionType=
String displayRowHint=Row2
Found View: UIKBKeyView
NAME=
numberPad-7
represented
String=7 display
String=7/PQRS displayType=
numberPad interactionType=
String displayRowHint=Row3
Found View: UIKBKeyView
NAME=
numberPad-8
represented
String=8 display
String=8/TUV displayType=
numberPad interactionType=
String displayRowHint=Row3
Found View: UIKBKeyView
NAME=
numberPad-9
represented
String=9 display
String=9/WXYZ displayType=
numberPad interactionType=
String displayRowHint=Row3
Found View: UIKBKeyView
NAME=
numberPad-Empty
represented
String= display
String= displayType=
numberPad interactionType=None displayRowHint=Row4
Found View: UIPeripheralHostView Found View: UIKeyboardAutomatic Found View: UIKeyboardImpl Found View: UIKeyboardLayoutStar Found View: UIKBKeyplaneView Found View: UIKBKeyView
NAME=Digit-1
represented
String=1 display
String=1 displayType=
String interactionType=
String-Popup displayRowHint=Row1 Found View: UIKBKeyView
NAME=Digit-2
represented
String=2 display
String=2 displayType=
String interactionType=
String-Popup displayRowHint=Row1 Found View: UIKBKeyView
NAME=Digit-3
represented
String=3 display
String=3 displayType=
String interactionType=
String-Popup displayRowHint=Row1 Found View: UIKBKeyView
NAME=Digit-4
represented
String=4 display
String=4 displayType=
String interactionType=
String-Popup displayRowHint=Row1 Found View: UIKBKeyView
NAME=Digit-5
represented
String=5 display
String=5 displayType=
String interactionType=
String-Popup displayRowHint=Row1 Found View: UIKBKeyView
NAME=Digit-6
represented
String=6 display
String=6 displayType=
String interactionType=
String-Popup displayRowHint=Row1 Found View: UIKBKeyView
NAME=Digit-7
represented
String=7 display
String=7 displayType=
String interactionType=
String-Popup displayRowHint=Row1 Found View: UIKBKeyView
NAME=Digit-8
represented
String=8 display
String=8 displayType=
String interactionType=
String-Popup displayRowHint=Row1 Found View: UIKBKeyView
NAME=Digit-9
represented
String=9 display
String=9 displayType=
String interactionType=
String-Popup displayRowHint=Row1 Found View: UIKBKeyView
NAME=Digit-0
represented
String
=0 display
String
=0 displayType=
String interactionType=
String-Popup displayRowHint=Row1 Found View: UIKBKeyView
NAME=Hyphen-Minus
represented
String=- display
String=- displayType=
String interactionType=
String-Popup Found View: UIKBKeyView
NAME=Solidus
represented
String=/ display
String=/ displayType=
String interactionType=
String-Popup displayRowHint=Row2 Found View: UIKBKeyView
NAME=Colon
represented
String=: display
String=: displayType=
String interactionType=
String-Popup displayRowHint=Row2 Found View: UIKBKeyView
NAME=Semicolon
represented
String=; display
String=; displayType=
String interactionType=
String-Popup Found View: UIKBKeyView
NAME=Left-Parenthesis
represented
String=( display
String=( displayType=
String interactionType=
String-Popup Found View: UIKBKeyView
NAME=Right-Parenthesis
represented
String=) display
String=) displayType=
String interactionType=
String-Popup Found View: UIKBKeyView
NAME=Pri@R_
197_11035@-Currency-Sign
represented
String=$ display
String=$ displayType=Dynamic
String interactionType=
String-Popup Found View: UIKBKeyView
NAME=Ampersand
represented
String=& display
String=& displayType=
String interactionType=
String-Popup Found View: UIKBKeyView
NAME=Commercial-At
represented
String=@ display
String=@ displayType=
String interactionType=
String-Popup Found View: UIKBKeyView
NAME=Full-Stop
represented
String=. display
String=. displayType=
String interactionType=
String-Popup Found View: UIKBKeyView
NAME=Comma
represented
String=, display
String=, displayType=
String interactionType=
String-Popup displayRowHint=Row3 Found View: UIKBKeyView
NAME=Question-Mark
represented
String=? display
String=? displayType=
String interactionType=
String-Popup Found View: UIKBKeyView
NAME=Exclamation-Mark
represented
String=! display
String=! displayType=
String interactionType=
String-Popup Found View: UIKBKeyView
NAME=Apostrophe
represented
String=' display
String=’ displayType=
String interactionType=
String-Popup Found View: UIKBKeyView
NAME=Quotation-Mark
represented
String=" display
String=” displayType=
String interactionType=
String-Popup Found View: UIKBKeyView
NAME=
delete-Key
represented
String=
delete display
String=
delete displayType=
delete interactionType=
delete Found View: UIKBKeyView
NAME=Return-Key
represented
String= interactionType=Return displayRowHint=Row2 Found View: UIKBKeyView
NAME=Undo-Key
represented
String=undo display
String=undo displayType=Command interactionType=Undo displayRowHint=Row3 Found View: UIKBKeyView
NAME=More-Key
represented
String=More display
String=more displayType=More interactionType=More displayRowHint=Row4 Found View: UIKBKeyView
NAME=Unlabeled-Space-Key
represented
String= display
String= displayType=Space interactionType=Space Found View: UIKBKeyView
NAME=More-Key
represented
String=More display
String=more displayType=More interactionType=More displayRowHint=Row4 Found View: UIKBKeyView
NAME=Dismiss-Key
represented
String=Dismiss display
String=Dismiss displayType=Dismiss interactionType=Dismiss Found View: UIKBKeyView
NAME=Shift-Key
represented
String=Shift display
String=shift displayType=Shift interactionType=Shift displayRowHint=Row3 Found View: UIKBKeyView
NAME=Shift-Key
represented
String=Shift display
String=shift displayType=Shift interactionType=Shift displayRowHint=Row3