在 iPhoneiPad 中随意修改数字键盘按钮

在 iPhoneiPad 中随意修改数字键盘按钮,第1张

概述一、起因 iPhone 的键盘,特别是数字键盘,往往不能满足程序的输入需要。最典型的例子就是在数字键盘上添加一个“.”,用来输入小数点。安装 iPhone SDK 官方的观点,如果要使用小数点键盘,那只好使用数字和符号键盘,但那样没个按键很小,且不需要的按键太多。 二、现有方案 针对这种情况,最早的解决方案,请参考这里:http://www.cnblogs.com/mac_arthur/archi

一、起因@H_404_7@@H_404_7@ iPhone 的键盘,特别是数字键盘,往往不能满足程序的输入需要。最典型的例子就是在数字键盘上添加一个“.”,用来输入小数点。安装 iPhone SDK 官方的观点,如果要使用小数点键盘,那只好使用数字和符号键盘,但那样没个按键很小,且不需要的按键太多。@H_404_7@@H_404_7@@H_404_7@ 二、现有方案@H_404_7@@H_404_7@ 针对这种情况,最早的解决方案,请参考这里:http://www.cnblogs.com/mac_arthur/archive/2010/05/18/1738363.html。使用的是 以下代码来检测一个 Notification。@H_404_7@

引用[[NSNotificationCenter defaultCenter] addobserver:self  @H_404_7@                                           selector:@selector(addCustomKeyBoardbutton) @H_404_7@                                                  name:UIKeyboarDWillShowNotification @H_404_7@                                                object:nil];

@H_404_7@@H_404_7@ 这种方法,在 iOS 4.0 后失效了,原因有两个,一是UIKeyboarDWillShowNotification的时候,键盘根本没有创建出来;另外,Class name 也被外包了一层,叫做UIPeripheralHostVIEw。@H_404_7@@H_404_7@ 于是又有了一种改进的方案,请看这里:http://www.neoos.ch/news/46-development/54-uikeyboardtypenumberpad-and-the-missing-return-key。主要的改进是在UIKeyboardDIDShowNotification的通知消息中来修改键盘。但正如作者所说,这里有个不完美的情况,只能等键盘动画显示完成之后,才能添加显示我们的东西,视觉效果不好。@H_404_7@@H_404_7@ 三、改进方案@H_404_7@@H_404_7@ 经过试验,找到一个比较完美的方案:在 UITextFIEld 的becomeFirstResponder和resignFirstResponder中修改键盘。@H_404_7@@H_404_7@ 定以一个类,假如叫做 KBCustomTextFIEld : UITextFIEld,在这个类中加入一下代码:@H_404_7@

引用 @H_404_7@ // @H_404_7@ - (BOol)becomeFirstResponder @H_404_7@ { @H_404_7@   BOol ret = [super becomeFirstResponder]; @H_404_7@   [self modifykeyvIEw:@"NumberPad-Empty" display:@"." represent:@"." interaction:@"String"]; @H_404_7@   return ret; @H_404_7@ } @H_404_7@@H_404_7@ // @H_404_7@ - (BOol)resignFirstResponder @H_404_7@ { @H_404_7@   BOol ret = [super resignFirstResponder]; @H_404_7@   [self modifykeyvIEw:@"NumberPad-Empty" display:nil represent:nil interaction:@"None"]; @H_404_7@   return ret; @H_404_7@ } @H_404_7@

@H_404_7@@H_404_7@ modifykeyvIEw 的实现可以参看附件。我使用的是循环查找UIKBkeyvIEw类,这是Apple 的 Private API(私有API的声明可以在这里找:http://github.com/kennytm/iphone-private-frameworks/tree/master/UIKit/),不确信能否通过 App Store 的审核:)@H_404_7@@H_404_7@ 更近一步地,我完善了一下 KBCustomTextFIEld,通过这个类,非常方便地就可以做到自定义输入键盘:

 

 

1. 使用小数点和数字键盘:非常简单,只要在 IB 中把 UITextFIEld 的类改成KBCustomTextFIEld,就OK了,不用一行代码,效果如下图: @H_404_7@

@H_404_7@@H_404_7@ 2. 在键盘的按键上添加一个文字按钮(并指定处理动作):设置KBCustomTextFIEld.kbDelegate,实现这两个函数即可: @H_404_7@@H_404_7@ 引用 @H_404_7@ // [Yonsm] Handle keyboard show @H_404_7@ - (voID)keyboardShow:(KBCustomTextFIEld *)sender @H_404_7@ { @H_404_7@   [sender addCustombutton:@"NumberPad-Empty" Title:@"DONE" target:self action:@selector(onbutton:)]; @H_404_7@ } @H_404_7@@H_404_7@ // [Yonsm] Handle keyboard hIDe @H_404_7@ - (voID)keyboardHIDe:(KBCustomTextFIEld *)sender @H_404_7@ { @H_404_7@   [sender delCustombutton:@"NumberPad-Empty"]; @H_404_7@ } @H_404_7@@H_404_7@

@H_404_7@@H_404_7@ 3. 更近一步地,这两个 Delegate 函数中,你只要通过 name 来查找到想要修改的 UIKBkeyvIEw,就可以随便修改它。name 可以通过 KBCustomTextFIEld 的#define _LOG_KEY_VIEW来列出所有的按键名称。name 为 nil 则找任何的 UIKBkeyvIEw(可以用他的 .supervIEw 来找到整个键盘VIEw,做更多的处理)。 @H_404_7@

下面是 iPhone 数字键盘的前面10个按键(后面忘了打出来了:) 引用 Found VIEw: UIPeripheralHostVIEw Found VIEw: UIKeyboardautomatic Found VIEw: UIKeyboardImpl Found VIEw: UIKeyboardLayoutStar Found VIEw: UIKBKeyplaneVIEw Found VIEw: UIKBkeyvIEw   name=NumberPad-1  representedString=1  displayString=1  displayType=NumberPad  interactionType=String  displayRowHint=Row1 Found VIEw: UIKBkeyvIEw   name=NumberPad-2  representedString=2  displayString=2/ABC  displayType=NumberPad  interactionType=String  displayRowHint=Row1 Found VIEw: UIKBkeyvIEw   name=NumberPad-3  representedString=3  displayString=3/DEF  displayType=NumberPad  interactionType=String  displayRowHint=Row1 Found VIEw: UIKBkeyvIEw   name=NumberPad-4  representedString=4  displayString=4/GHI  displayType=NumberPad  interactionType=String  displayRowHint=Row2 Found VIEw: UIKBkeyvIEw   name=NumberPad-6  representedString=6  displayString=6/MNO  displayType=NumberPad  interactionType=String  displayRowHint=Row2 Found VIEw: UIKBkeyvIEw   name=NumberPad-7  representedString=7  displayString=7/PQRS  displayType=NumberPad  interactionType=String  displayRowHint=Row3 Found VIEw: UIKBkeyvIEw   name=NumberPad-8  representedString=8  displayString=8/TUV  displayType=NumberPad  interactionType=String  displayRowHint=Row3 Found VIEw: UIKBkeyvIEw   name=NumberPad-9  representedString=9  displayString=9/WXYZ  displayType=NumberPad  interactionType=String  displayRowHint=Row3 Found VIEw: UIKBkeyvIEw   name=NumberPad-Empty  representedString=  displayString=  displayType=NumberPad  interactionType=None  displayRowHint=Row4 代码对 iPad 也有效,当然 Key name 和  Type 不一样,下面是 iPad 数字键盘的Log: 引用Found VIEw: UIPeripheralHostVIEw Found VIEw: UIKeyboardautomatic Found VIEw: UIKeyboardImpl Found VIEw: UIKeyboardLayoutStar Found VIEw: UIKBKeyplaneVIEw Found VIEw: UIKBkeyvIEw   name=Digit-1  representedString=1  displayString=1  displayType=String  interactionType=String-Popup  displayRowHint=Row1 Found VIEw: UIKBkeyvIEw   name=Digit-2  representedString=2  displayString=2  displayType=String  interactionType=String-Popup  displayRowHint=Row1 Found VIEw: UIKBkeyvIEw   name=Digit-3  representedString=3  displayString=3  displayType=String  interactionType=String-Popup  displayRowHint=Row1 Found VIEw: UIKBkeyvIEw   name=Digit-4  representedString=4  displayString=4  displayType=String  interactionType=String-Popup  displayRowHint=Row1 Found VIEw: UIKBkeyvIEw   name=Digit-5  representedString=5  displayString=5  displayType=String  interactionType=String-Popup  displayRowHint=Row1 Found VIEw: UIKBkeyvIEw   name=Digit-6  representedString=6  displayString=6  displayType=String  interactionType=String-Popup  displayRowHint=Row1 Found VIEw: UIKBkeyvIEw   name=Digit-7  representedString=7  displayString=7  displayType=String  interactionType=String-Popup  displayRowHint=Row1 Found VIEw: UIKBkeyvIEw   name=Digit-8  representedString=8  displayString=8  displayType=String  interactionType=String-Popup  displayRowHint=Row1 Found VIEw: UIKBkeyvIEw   name=Digit-9  representedString=9  displayString=9  displayType=String  interactionType=String-Popup  displayRowHint=Row1 Found VIEw: UIKBkeyvIEw   name=Digit-0  representedString=0  displayString=0  displayType=String  interactionType=String-Popup  displayRowHint=Row1 Found VIEw: UIKBkeyvIEw   name=Hyphen-Minus  representedString=-  displayString=-  displayType=String  interactionType=String-Popup   Found VIEw: UIKBkeyvIEw   name=SolIDus  representedString=/  displayString=/  displayType=String  interactionType=String-Popup  displayRowHint=Row2 Found VIEw: UIKBkeyvIEw   name=Colon  representedString=:  displayString=:  displayType=String  interactionType=String-Popup  displayRowHint=Row2 Found VIEw: UIKBkeyvIEw   name=Semicolon  representedString=;  displayString=;  displayType=String  interactionType=String-Popup   Found VIEw: UIKBkeyvIEw   name=left-Parenthesis  representedString=(  displayString=(  displayType=String  interactionType=String-Popup   Found VIEw: UIKBkeyvIEw   name=Right-Parenthesis  representedString=)  displayString=)  displayType=String  interactionType=String-Popup   Found VIEw: UIKBkeyvIEw   name=Primary-Currency-Sign  representedString=$  displayString=$  displayType=DynamicString  interactionType=String-Popup Found VIEw: UIKBkeyvIEw   name=Ampersand  representedString=&  displayString=&  displayType=String  interactionType=String-Popup   Found VIEw: UIKBkeyvIEw   name=Commercial-At  representedString=@  displayString=@  displayType=String  interactionType=String-Popup   Found VIEw: UIKBkeyvIEw   name=Full-Stop  representedString=.  displayString=.  displayType=String  interactionType=String-Popup   Found VIEw: UIKBkeyvIEw   name=Comma  representedString=,  displayString=,  displayType=String  interactionType=String-Popup  displayRowHint=Row3 Found VIEw: UIKBkeyvIEw   name=Question-Mark  representedString=?  displayString=?  displayType=String  interactionType=String-Popup   Found VIEw: UIKBkeyvIEw   name=Exclamation-Mark  representedString=!  displayString=!  displayType=String  interactionType=String-Popup   Found VIEw: UIKBkeyvIEw   name=Apostrophe  representedString='  displayString=’  displayType=String  interactionType=String-Popup   Found VIEw: UIKBkeyvIEw   name=Quotation-Mark  representedString="  displayString=”  displayType=String  interactionType=String-Popup   Found VIEw: UIKBkeyvIEw   name=Delete-Key  representedString=Delete  displayString=delete  displayType=Delete  interactionType=Delete   Found VIEw: UIKBkeyvIEw   name=Return-Key  representedString= interactionType=Return  displayRowHint=Row2 Found VIEw: UIKBkeyvIEw   name=Undo-Key  representedString=undo  displayString=undo  displayType=Command  interactionType=Undo  displayRowHint=Row3 Found VIEw: UIKBkeyvIEw   name=More-Key  representedString=More  displayString=more  displayType=More  interactionType=More  displayRowHint=Row4 Found VIEw: UIKBkeyvIEw   name=Unlabeled-Space-Key  representedString=   displayString=  displayType=Space  interactionType=Space   Found VIEw: UIKBkeyvIEw   name=More-Key  representedString=More  displayString=more  displayType=More  interactionType=More  displayRowHint=Row4 Found VIEw: UIKBkeyvIEw   name=dismiss-Key  representedString=dismiss  displayString=dismiss  displayType=dismiss  interactionType=dismiss   Found VIEw: UIKBkeyvIEw   name=Shift-Key  representedString=Shift  displayString=shift  displayType=Shift  interactionType=Shift  displayRowHint=Row3 Found VIEw: UIKBkeyvIEw   name=Shift-Key  representedString=Shift  displayString=shift  displayType=Shift  interactionType=Shift  displayRowHint=Row3 总结

以上是内存溢出为你收集整理的在 iPhone/iPad 中随意修改数字键盘按钮全部内容,希望文章能够帮你解决在 iPhone/iPad 中随意修改数字键盘按钮所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1090908.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存