Ios-uitextview and keyboard recovery and keyboard occlusion input box

Source: Internet
Author: User

First, Uitextview

You can implement multiple lines of input text box, the basic properties and Uitextfield similar, you can enter multiple lines, you can scroll.
Uitextview, there's another way of acting.
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

Can control the number of input text, more commonly

 #pragma the proxy method for Mark UitextviewWhether you can start editing-(BOOL) Textviewshouldbeginediting: (Uitextview *) TextView {NSLog (@ "%s", __func__);ReturnYES; }Whether you can end the edit-(BOOL) Textviewshouldendediting: (Uitextview *) TextView {NSLog (@ "%s", __func__);ReturnYES; }has started editing-(void) Textviewdidbeginediting: (Uitextview *) TextView {NSLog (@ "%s", __func__); }has finished editing-(void) Textviewdidendediting: (Uitextview *) TextView {NSLog (@ "%s", __func__); }Content Changes-(void) Textviewdidchange: (Uitextview *) TextView {NSLog (@ "%s", __func__); }Cursor Change-(void) Textviewdidchangeselection: (Uitextview *) TextView {NSLog (@ "%s", __func__); }The current input position, the current input text, whether you can continue to enter-(BOOL) TextView: (Uitextview *) TextView Shouldchangetextinrange: (Nsrange) Range Replacementtext: (NSString *) Text {NSLog (@ "Length =%ld,location =%ld", Range. length,range. location);NSLog (@ "Text =%@", text);ReturnYES; }The following two are not easy to understand, presumably to verify the URL and file name suffixAsks the delegate if the specified text view should allow user interaction with the given URL in the given range of text . - (BOOL) TextView: (Uitextview *) TextView Shouldinteractwithurl: (Nsurl *) URL InRange: (nsrange) characterrange Ns_available_ios (7_0) { NSLog (@ "url=%@", url. host); NSLog (@ "url touch"); return YES;}-(BOOL) TextView: (uitextview *) TextView shouldinteractwithtextattachment: (  Nstextattachment *) textattachment inrange: (nsrange) characterrange Ns_available_ios (7_0) {  return YES;}                  
Second, keyboard recovery and keyboard occlusion input box

When using the input text box, there is often a problem, the keyboard pop-up block the text box, you can move the input box position to avoid such a situation.
The idea is relatively simple, registration notification to monitor the keyboard pop-up and disappearance events, and then implement the corresponding method, in the keyboard pop-up or disappear, change the original view frame
To move the view up or down one of the keyboard heights, the keyboard does not block the view

First sign up to notify the simplified keyboard popup event

//注册通知,监听键盘弹出事件  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:)      name:UIKeyboardWillShowNotification object:nil]; //注册通知,监听键盘消失事件 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHidden) name:UIKeyboardDidHideNotification object:nil];

When the keyboard pops up or disappears, the trigger method moves the input box.

// 键盘弹出时调用方法  -(void)keyboardDidShow:(NSNotification *)notification  //键盘消失时调用  -(void)keyboardDidHidden

Then set a touch event and tap the blank to retract the keyboard

//点击屏幕空白处  -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  {      //回收键盘,两者方式 //UITextView *textView = (UITextView*)[self.view viewWithTag:1001]; //[textView resignFirstResponder]; [self.view endEditing:YES]; NSLog(@"touch"); }

Full code:

#import"FirstViewController.h"@implementationFirstviewcontroller-(void) Viewdidload {[Super Viewdidload];#pragma Mark UitextviewUitextfield:To inherit Uicontrol, you can enter only one row, not scroll, and set the reminder text.Have return proxy method and ClearbuttonmodeUitextview:Can enter multiple lines, you can scroll, you can not set the reminder text.Uitextview *textview = [[Uitextview Alloc] initWithFrame:CGRectMake (10,10,280,80)];Initialize TextView. backgroundcolor=[Uicolor colorwithred:0.21 Green:0.71 Blue:0.51 Alpha:0.5];Background color TextView. scrollenabled =YES;Whether the text is allowed to slide when it exceeds the border of the view, the default is "YES" TextView. Editable =YES;Allow editing of content, default to "YES" TextView. font=[Uifont Fontwithname:@ "Arial" Size:18.0];Set the font name and font size; TextView. Returnkeytype =Uireturnkeydefault;Type of Return key TextView. Keyboardtype =Uikeyboardtypedefault;Keyboard type TextView. TextAlignment =Nstextalignmentleft;The location of the text display defaults to the left TextView. Datadetectortypes =Uidatadetectortypeall;Display the connection mode of the data type (e.g. phone number, URL, address, etc.) TextView. TextColor = [Uicolor Blackcolor]; TextView. Delegate =SelfSet the implementation class for the Proxy method TextView. Text =@ "Uitextview";Set the displayed text content [TextView. Layer Setcornerradius:10];Set rounded corners TextView. Tag =1001;Set Tag ValueTo add a listening event for the keyboardRegistering notifications, listening for keyboard popup events [[Nsnotificationcenter Defaultcenter] Addobserver:Self selector:@selector (keyboarddidshow:) Name:Uikeyboarddidshownotification object:NIL];Registering notifications, listening for keyboard vanishing events [[Nsnotificationcenter Defaultcenter] Addobserver:Self selector:@selector (Keyboarddidhidden) Name:Uikeyboarddidhidenotification object:NIL];Add a sub view to the view, set the tag value for this child view to 1000, add a TextView and a Send button on this view,If we want to reach the TextView keyboard pop-up, the entire view is shifted up, the keyboard disappears, the view pans down, and the interface that sends the SMS is simulated.UIView *keyview = [[UIView Alloc]initwithframe:CGRectMake (0,567,375,100)]; KeyView. backgroundcolor = [Uicolor colorwithred:0.2 Green:0.3 Blue:0.6 Alpha:0.5]; KeyView. Tag =1000; [KeyView Addsubview:textview]; [Self. View Addsubview:keyview]; }#pragma Mark's approach to triggering when a keyboard change is heardWhen the keyboard pops up-(void) Keyboarddidshow: (Nsnotification *) Notification {Get keyboard heightNsvalue *keyboardobject = [[Notification UserInfo] Objectforkey:Uikeyboardframeenduserinfokey];NSLog (@ "%@", keyboardobject);CGRect Keyboardrect; [Keyboardobject getvalue:&keyboardrect];Get the height of the keyboardCGRect keyboardrect = [[Notification.userinfo objectforkey:uikeyboardframeenduserinfokey]cgrectvalue];Gets the animation time of the keyboard so that it can be more coherent when the view is moved upDouble duration = [[Notification. UserInfo Objectforkey:Uikeyboardanimationdurationuserinfokey] Doublevalue];NSLog (@ "%f", duration);Adjust the location of the view where the TextView is placedSet animation [UIView beginanimations:Nil Context:NIL];Define animation time [UIView Setanimationduration:duration]; [UIView Setanimationdelay:0];Set the frame of the view and pan up [(UIView *) [Self. View Viewwithtag:Setframe]:CGRectMake (0,Self. View. Frame. Size. height-keyboardrect. Size. height-100,375,100)];Commit animation [UIView Commitanimations]; }When the keyboard disappears-(void) Keyboarddidhidden {Defining animations[UIView Beginanimations:nil Context:nil];[UIView setanimationduration:0.25];Set the frame of the view and pan down [(UIView *) [Self. View Viewwithtag:1000] setframe:cgrectmake (0, Span class= "Hljs-number" >567, 375, 100)]; //[UIView commitanimations];} //click on the screen space-(void) Touchesbegan: ( nsset *) touches withevent: (uievent *) event {//recovery keyboard, two ways //uitextview *textview = (uitextview*) [Self.view ViewWithTag : 1001]; //[textview Resignfirstresponder]; [self.view endediting:YES]; Span class= "hljs-built_in" >nslog (@ "Touch");}  @end             

Back again in TableView inside add TextView, not find the TextView to Resignkeyboard, use a can get TextView to Becomefirst, and then Resignfirst

And a way to get the first responder directly.
UIWindow Keywindow = [[UIApplication sharedapplication] Keywindow];
UIView
firstresponder = [Keywindow performselector: @selector (FirstResponder)];
[FirstResponder Resignfirstresponder];

Reprint: Http://www.jianshu.com/p/1bf3407b15f7

Ios-uitextview and keyboard recovery and keyboard occlusion input box

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.