HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在 iPhone/iPad中随意修改数字键盘按钮大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

iPhone 的键盘,特别是数字键盘,往往不能满足程序的输入需要。最典型的例子就是在数键盘添加一个“.”,用来输入小数点。安装 iPhone SDK 官方的观点,如果要使用小数点键盘,那只好使用数字和符号键盘,但那样没个按键很小,且不需要的按键太多。

二、现有方案

针对这种情况,最早的解决方案,请参这里:http://www.cnblogs.com/mac_arthur/archive/2010/05/18/1738363.html。使用的是 以下代码来检测一个 Notification。



这种方法,在 iOS 4.0 后失效了,原因有两个,一是UIKeyboardWillShowNotification的时候,键盘根本没有创建出来;另外,Class Name 也被外包了一层,叫做UIPeripheralHostView。

于是又有了一种改进的方案,请看这里:http://www.neoos.ch/news/46-development/54-uikeyboardtypenumberpad-and-the-missing-return-key。主要的改进是在UIKeyboardDidShowNotification的通知消息中来修改键盘。但正如作者所说,这里有个不完美的情况,只能等键盘动画显示完成之后,才能添加显示我们的东西,视觉效果不好。

三、改进方案

经过试验,找到一个比较完美的方案:在 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,通过这个类,非常方便地就可以做到自定义输入键盘

KBCustom.zip

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

在 iPhone/iPad中随意修改数字键盘按钮



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"];
}


在 iPhone/iPad中随意修改数字键盘按钮



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

在 iPhone/iPad中随意修改数字键盘按钮




下面是 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 &nbs
p;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中随意修改数字键盘按钮所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。