(Add to favorites) freely modify the keyboard buttons on the iPhone/iPad

Source: Internet
Author: User
I. Cause

IPhone keyboards, especially numeric keyboards, often cannot meetProgram. The most typical example is to add a "." On the keyboard to enter the decimal point. According to the official idea of installing the iPhone SDK, if you want to use the decimal keyboard, you have to use the number and symbol keyboard, but there are no keys that are small and there are too many buttons that are not needed.

Ii. Existing solutions

For the earliest solutions, see http://www.cnblogs.com/mac_arur/archive/2010/05/18/1738363.html. Use the following Code To detect a notification. Reference [[nsicationicationcenter defacenter center] addobserver: Self
Selector: @ selector (addcustomkeyboardbutton)
Name: uikeyboardwillshownotification
Object: Nil];

This method fails after IOS 4.0 for two reasons: uikeyboardWillDuring shownotification, the keyboard is not created at all. In addition, the class name is also outsourced to a layer called uiperipheralhostview.

So there is another improvement solution. Please refer to the following. The main improvement is in the uikeyboardDidIn the shownotification notification message, modify the keyboard. But as the author said, there is an imperfect situation here. We can only add and display our items after the keyboard animation is displayed. The visual effect is not good.

3. Improvement Plan

After testing, we found a perfect solution: Modify the keyboard in the becomefirstresponder and resignfirstresponder of uitextfield.

It is set to a class, for example, kbcustomtextfield: uitextfield, add the code: Reference //
-(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;
}

For the implementation of modifykeyview, see the attachment. I am using the loop lookup uikbkeyview class, which is Apple's private API (Private API Declaration can be found here: http://github.com/kennytm/iphone-private-frameworks/tree/master/UIKit/), not sure whether it can pass App Store review :)

One step closer, I improved kbcustomtextfield. Through this class, you can easily customize the Input Keyboard:

Download file (51 downloads) Click here to download the file: kbcustom.zip

1. Use the decimal point and numeric keyboard: It is very simple. As long as you change the uitextfield class to kbcustomtextfield in IB, it will be OK. You don't need a line of code. The effect is as follows:

2. Add a text button on the keyboard button (and specify the processing action): Set kbcustomtextfield. kbdelegate to implement these two functions:

Reference // [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. In the two delegate functions, you only need to use the name to find the uikbkeyview you want to modify. You can use # DEFINE _ log_key_view of kbcustomtextfield to list all key names. If the name is nil, find any uikbkeyview (you can use its. superview to find the entire keyboard view for more processing ).

Below are the first 10 buttons on the iPhone digital keyboard (I forgot to press them later :)

Reference 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

The Code is also valid for iPad. Of course, the key name and type are different. below is the log of the iPad digital keyboard:

Reference 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 = inclucial-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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.