Ios:iphone keyboard notification and keyboard customization

Source: Internet
Author: User
<span id="Label3"></p>I. Keyboard notifications<p><p>When the text view (such as uitextfield,uitextview, input box inside Uiwebview) enters edit mode to become first responder, the keyboard is automatically displayed. Becoming FirstResponder can be triggered by a user clicking on the trigger or sending a becomefirstresponder message to a text view. When the text view exits first responder, the keyboard disappears. Text view exit first responder may be triggered by the user tapping the done or return key on the keyboard to end the input, or by sending a resignfirstresponder message to the text view.</p></p><p><p>When the keyboard is displayed or disappears, the system sends the relevant Notification:<br>Uikeyboardwillshownotification<br>Uikeyboarddidshownotification<br>Uikeyboardwillhidenotification<br>Uikeyboarddidhidenotification</p></p><p><p>The UserInfo dictionary in the notification message nsnotification contains the location and size information for the keyboard, and the corresponding key is</p></p><p><p>Uikeyboardframebeginuserinfokey<br>Uikeyboardframeenduserinfokey<br>Uikeyboardanimationdurationuserinfokey<br>Uikeyboardanimationcurveuserinfokey</p></p><p><p>The uikeyboardframebeginuserinfokey,uikeyboardframeenduserinfokey corresponding value is a Nsvalue object that contains the CGRect structure internally. Position information, respectively, When the keyboard starts and when it terminates.</p></p><p><p>The value of Uikeyboardanimationcurveuserinfokey corresponds to the NSNumber object, which is an internal uiviewanimationcurve type of data that represents the type of animation that the keyboard displays or Disappears.</p></p><p><p>The value of Uikeyboardanimationdurationuserinfokey corresponds to the NSNumber object, which is a double-type data that represents the duration of the animation when the keyboard h is displayed or Disappears.</p></p><p><p>For example, the userinfo content in the Uikeyboardwillshownotification,uikeyboarddidshownotification notification is</p></p><pre><pre><code>userInfo = { UIKeyboardAnimationCurveUserInfoKey = 0; UIKeyboardAnimationDurationUserInfoKey = "0.25"; UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 216}}"; UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 588}"; UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 372}"; UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 480}, {320, 216}}"; UIKeyboardFrameChangedByUserInteraction = 0; UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 264}, {320, 216}}";}</code></pre></pre><p><p>In the Uikeyboardwillhidenotification,uikeyboarddidhidenotification notification<br>UserInfo content Is:</p></p><pre><pre><code>userInfo = { UIKeyboardAnimationCurveUserInfoKey = 0; UIKeyboardAnimationDurationUserInfoKey = "0.25"; UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 216}}"; UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 372}"; UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 588}"; UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 264}, {320, 216}}"; UIKeyboardFrameChangedByUserInteraction = 0; UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 480}, {320, 216}}";}</code></pre></pre><p><p></p></p><p><p>On text,web, and Editing Programming Guide for ios, There are descriptions note:the rectangle contained in the Uikeyboardframebeginuserinfoke Y and Uikeyboardframeenduserinfokey properties of the UserInfo dictionary should be used only for the size information it Contains. The origin of the rectangle (which is always {0.0, 0.0}) in Rectangle-intersection operations. Because the keyboard is animated to position, the actual bounding rectangle of the keyboard changes over Time.<br>however, from the actual information obtained, the origin of the rectangle is not {0.0, 0.0}, this should not be Accurate.</p></p>Two. text objects and WebView keyboard settings<p><p>Both uitextfiled and Uitextview follow the uitextinputtraits protocol, which defines the properties of the keyboard set in the Uitextinputtraits protocol, with</p></p><p><p>1. keyboardtype: keyboard type, such as uikeyboardtypedefault,uikeyboardtypeurl,uikeyboardtypeemailaddress, uikeyboardtypephonepad, etc.<br></p></p><p><p>2. Returnkeytype: the text that the keyboard return key displays, The default is "return", other optional include go,next,done,send,google and so On.</p></p><p><p>3. keyboardappearance: keyboard appearance, The default is uikeyboardappearancedefault, that is, in the light blue opaque background, There is another one for uikeyboardappearancealert,<br>The keyboard background is black translucent for display when the warning box is entered, such as when entering a password in appstore:</p></p><p><p></p></p><p><p>If you want to display a black keyboard and do not want to reveal the bottom view transparently, you can configure the keyboard to be alert type, and then listen to the broadcast notification displayed by the keyboard, adding an opaque black background view at the bottom of the keyboard when the keyboard is Displayed.</p></p><p><p>Note: in Apple's Keyboard sample program keyboardaccessory, to change the Uitextview keyboard type to uikeyboardappearancealert, running the keyboard in the ipad simulator does not appear to have a black transparent effect, do you know why? Uikeyboardappearancealert settings in iphone are Valid.</p></p><p><p>4.autocapitalizationType: text case style, See Uitextautocapitalizationtype.<br>5.autocorrectionType: if Auto correct, see Uitextautocorrectiontype.<br>6.spellCheckingType: spell check settings, see Uitextspellcheckingtype.<br>7. Enablesreturnkeyautomatically: Whether to disable the return key when there is no text, the default is No. If yes, the user enters at least one word specifier return key to be Activated.<br>8. Securetextentry: If you enter a password, you can set this type to yes, you can display the last character when you enter a character, and other characters appear as Points.</p></p><p><p>The UIWebView itself does not follow the Uitextinputtraits protocol directly, but it can also set the keyboard properties of its internal input parts. As described in configuring the Keyboard for Web Views.</p></p><p><p>Set autocorrect, auto-capitalization Property.</p></p><pre><pre><code><input type="text" size="30" autocorrect="off" autocapitalization="on"></code></pre></pre><p><p>To set the keyboard type:</p></p><pre><pre><code>Text: <input type="text"></input>Telephone: <input type="tel"></input>URL: <input type="url"></input>Email: <input type="email"></input>Zip code: <input type="text" pattern="[0-9]*"></input></code></pre></pre>Three. Adjust the text view position with keyboard notifications<p><p>When the text view becomes first responser, the keyboard appears at the bottom of the window, and the keyboard that appears is likely to cover the text view so you can't see the editing effect. One of the key uses of keyboard notifications is to get the position information of the keyboard when the keyboard is displayed or hidden, so that you can adjust the position or size of the text view in the window so that it can be displayed above the Keyboard.</p></p><p><p>Text, Web, and Editing Programming Guide for iOS moving<br>Content that is Located under the keyboard section in the keyboard display and disappearance notification by adjusting the contents Uiscrollview view Contentinset and Contentoffset to ensure that the edited text view is not covered by the Keyboard.</p></p><p><p>Its process is<br>1. When initializing (viewdidload or viewwillappear), register the method that handles keyboard notifications.</p></p><pre><pre><code>- (void)registerForKeyboardNotifications{ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];}</code></pre></pre><p><p>2. In the notification event processing that is displayed on the keyboard, get the size of the keyboard that is about to be displayed, set the contentinset of Uiscrollview to the frame area of the keyboard, and also set the scrollindicatorinsets to ensure that the scroll bar is not covered by the Keyboard. Gets the origin position of the edit text view, compared to the keyboard display area, if it is overwritten by the keyboard, adjust the contentoffset so that it is above the keyboard</p></p><pre><pre><code>// Called when the UIKeyboardDidShowNotification is sent.- (void)keyboardWasShown:(NSNotification*)aNotification{ NSDictionary* info = [aNotification userInfo]; CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0); scrollView.contentInset = contentInsets; scrollView.scrollIndicatorInsets = contentInsets; // If active text field is hidden by keyboard, scroll it so it‘s visible // Your application might not need or want this behavior. CGRect aRect = self.view.frame; aRect.size.height -= kbSize.height; if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) { CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height); [scrollView setContentOffset:scrollPoint animated:YES]; }}</code></pre></pre><p><p>3. In the notification processing event that the keyboard disappears, simply restore the Uiscrollview to</p></p><pre><pre><code>// Called when the UIKeyboardWillHideNotification is sent- (void)keyboardWillBeHidden:(NSNotification*)aNotification{ UIEdgeInsets contentInsets = UIEdgeInsetsZero; scrollView.contentInset = contentInsets; scrollView.scrollIndicatorInsets = contentInsets;}</code></pre></pre><p><p>In the Apple Keyboardaccessory sample program, You also demonstrated the code that uses keyboard notifications to adjust the position of the text view, which uses keyboard animation time in the keyboard notification to synchronize the animation of the text view movement with the display and disappearance of the Keyboard.</p></p><pre><code>-(void) keyboardwillshow: (nsnotification *) notification {/* Reduce The size of the text view so it's not OBSC Ured by the Keyboard. Animate The resize so, it's in sync with the appearance of the Keyboard. */nsdictionary *userinfo = [notification userInfo]; Get The origin of the keyboard when it ' s Displayed. nsvalue* avalue = [userInfo objectforkey:uikeyboardframeenduserinfokey]; Get the top of the keyboard as the Y coordinate of its origin in Self's view ' s coordinate system. The bottom of the text view ' s frame should align with the top of the keyboard ' s final Position. CGRect keyboardrect = [avalue cgrectvalue]; Keyboardrect = [self.view Convertrect:keyboardrect fromview:nil]; CGFloat keyboardtop = keyboardrect.origin.y; CGRect Newtextviewframe = self.view.bounds; NewTextViewFrame.size.height = keyboardtop-self.view.bounds.origin.y; Get the duration of the Animation. Nsvalue *animationdurationvalue = [userInfo OBJECTForkey:uikeyboardanimationdurationuserinfokey]; Nstimeinterval animationduration; [animationdurationvalue getvalue:&animationduration]; Animate The resize of the text view ' s frame in sync with the keyboard ' s appearance. [UIView Beginanimations:nil context:null]; [UIView setanimationduration:animationduration]; Textview.frame = newtextviewframe; [UIView commitanimations];} -(void) keyboardwillhide: (nsnotification *) notification {nsdictionary* userInfo = [notification userInfo]; /* Restore the size of the text view (fill self ' s view). Animate The resize so, it's in sync with the disappearance of the Keyboard. */nsvalue *animationdurationvalue = [userInfo objectforkey:uikeyboardanimationdurationuserinfokey]; Nstimeinterval animationduration; [animationdurationvalue getvalue:&animationduration]; [UIView Beginanimations:nil context:null]; [UIView setanimationduration:animationduration]; Textview.frame = Self.view.bounds; [UIView commitanimations];}</code></pre><p><p>For code that adjusts the size of the text view, you can also refer to different-show keyboard and resize UIView with the text field.</p></p><p><p>You can also implement a custom keyboard in keyboard notifications, such as Uikeyboardtypenumberpad and the MISSING "return" key, which uses keyboard notifications to add the RETURN key to the numeric keypad. Also refer to Uikeyboardtypenumberpad keyboard to add return key<br></p></p><p><p>Note: If there are two Uitextfield in the same window, and when the first Uitextfield becomes primary responser, the keyboard is displayed and the uikeyboardwillshownotification is received, Uikeyboarddidshownotification notifies you that when first Responser is transferred to the second uitextfield, the keyboard will always be displayed, and you will not receive Willshow and Didshow notifications at this Time.</p></p>Four. customizing input views with Inputaccessoryview and Inputview<p><p>The Inputaccessoryview and Inputview properties are defined in Uiresponder as ReadOnly properties, but are redefined in uitextfiled and Uitextview in order to ReadWrite properties that can be assigned by the User. If Inputview is not nil, then when the text view becomes first responder, the system keyboard is not displayed, but the custom Inputview is displayed; if Inputaccessoryview does not<br>Is nil, the Inputaccessoryview is displayed above the system keyboard or custom INPUTVIEW. When using inputview, there will still be willshow,didshow,willhide,didhide keyboard notifications, beginframe and Endframe in the notification system keyboard (or Inputview) Frame together with the INPUTACCESSORYVIEW.</p></p><p><p>Custom Inputaccessoryview are very common, such as when editing text messages in the input box<br><br><br>Web page Input Keyboard<br><br><br>Sina Weibo review interface<br></p></p><p><p>If you want to enter without using the system keyboard and use a custom keyboard, you can set inputview, such as the keyboard when the amount entered in the Notebook<br><br>If you do not want to use the keyboard input, want to choose from Uipickerview or uidatepicker, you can set Inputview to the corresponding picker view,<br></p></p><p><p>The Apple Keyboard sample program Keyboardaccessory demonstrates how Inputaccessoryview is Used.<br>Ios,want a "button" above a uipicker or Keyboard, Inputview,inputaccessoryview demonstrates the code that adds Uipickerview on top of toolbar.</p></p><p><p>In Standard view only Uitextfield and Uitextview redefine Inputview and Inputaccessoryview in order to ReadWrite type, if you want to use in custom views, You need to redefine the Inputview and Inputaccessoryview properties in the custom VIEW. See input view and input accessory views.</p></p><p><p>Reference:<br>Text, Web, and Editing Programming Guide for ios–managing the Keyboard<br>text,web, and Editing Programming Guide for Ios–input views and Input accessory views<br>Uitextview Class Reference<br>Uitextfield Class Reference<br>Uiresponder Class Reference<br>UIWindow Class Reference<br>Uitextinputtraits Protocol Reference<br>Keyboardaccessory Demo<br>Different-show Keyboard and resize UIView with text field<br>iOS, want a "button" above a uipicker or Keyboard, inputview, Inputaccessoryview<br>Uikeyboardframebeginuserinfokey Uikeyboardframeenduserinfokey<br>Uikeyboard Keyboard related Knowledge point-ios development<br>Change Uisearchbar/keyboard Search Button Title<br>IOS Human Interface Guidelines keyboards and Input views<br>How do I change keyboard background color in iOS?<br>Favorites UI Design</p></p><p><p>Ios:iphone keyboard notification and keyboard customization</p></p></span>

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.