When you encounter a requirement in a project, there is a text box that needs to be automatically incremented as the text entered by the user increases in height.
For example, when the user enters less than one line of text, the height of the TextView is the initial height,
When you enter more than one line of text, we adjust the height of the TextView to show the height of the two lines of text.
Here, we want to implement a comment function, but also the input box to follow the keyboard to move.
Start code
First, we create a new class, specifically to manage the input box, we named:commentview inherit UIView
Create a Uitextview for him (our input box)
#import <UIKit/UIKit.h> @interface commentview:uiview@property (nonatomic,strong) Uitextview *textview;-(void) Inittextframe; @end
Next we initialize the input box in the implementation class
Gets the screen width, height # define SCREEN_FRAME ([UIScreen mainscreen].bounds) #define SCREEN_WIDTH ([UIScreen mainscreen]. Bounds.size.width) #define Screen_height ([UIScreen mainscreen].bounds.size.height) #import "CommentView.h" @ Implementation commentview-(ID) initWithFrame: (cgrect) frame{ self=[super initwithframe:frame]; if (self) { self.backgroundcolor=[uicolor bluecolor]; [Self inittextview:frame]; } return self;} -(void) Inittextview: (cgrect) frame{ Self.textview=[[uitextview alloc]initwithframe:cgrectmake (2, 2, SCREEN_ Width-2*2, frame.size.height-2*2)]; Self.textview.backgroundcolor=[uicolor colorwithred:233.0/255 green:232.0/255 blue:250.0/255 alpha:1.0]; [Self AddSubview:self.textView]; } @end
Let's add a little bit of Commentview to see the effect.
We add a button that opens Comentview when the button is clicked
Gets the screen width, height # define SCREEN_FRAME ([UIScreen mainscreen].bounds) #define SCREEN_WIDTH ([UIScreen mainscreen]. Bounds.size.width) #define Screen_height ([UIScreen mainscreen].bounds.size.height) #import "ViewController.h" # Import "CommentView.h" @interface Viewcontroller () {Commentview *commentv;} @end @implementation viewcontroller-(void) viewdidload {[Super viewdidload]; Additional setup after loading the view, typically from a nib. UIButton *testbtn=[[uibutton Alloc]initwithframe:cgrectmake (10, 120, 88, 36)]; [Testbtn settitle:@ "testbtn" forstate:uicontrolstatenormal]; [Testbtn Settitlecolor:[uicolor Greencolor] forstate:uicontrolstatenormal]; [Testbtn addtarget:self Action: @selector (Opentestview) Forcontrolevents:uicontroleventtouchdown]; [Self.view addsubview:testbtn];} -(void) opentestview{Commentv=[[commentview alloc]initwithframe:cgrectmake (0, screen_height-40, SCREEN_WIDTH, 40)]; [Self.view Addsubview:commentv]; }-(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} @end
Run, click button to eject the Commentview effect as follows
Next we have the text box follow the keyboard
We're opening the Contentview, open the keyboard.
Then add the keyboard to open and close the notification, after hearing the notification, adjust the location of the Commentview
[[Nsnotificationcenter defaultcenter]addobserver:self selector: @selector (keyboardshow:) Name: Uikeyboardwillshownotification Object:nil]; [[Nsnotificationcenter defaultcenter]addobserver:self selector: @selector (keyboardhide:) Name: Uikeyboardwillhidenotification Object:nil];
#pragma mark keyboardnotification-(void) Keyboardshow: (nsnotification *) note{ cgrect keyboardrect=[ Note.userinfo[uikeyboardframeenduserinfokey] Cgrectvalue]; CGFloat deltay=keyboardrect.size.height; [UIView Animatewithduration:[note.userinfo[uikeyboardanimationdurationuserinfokey] floatvalue] animations:^{ Commentv.transform=cgaffinetransformmaketranslation (0,-deltay); }];} -(void) Keyboardhide: (nsnotification *) note{ [UIView animatewithduration:[note.userinfo[ Uikeyboardanimationdurationuserinfokey] Floatvalue] animations:^{ commentv.transform= cgaffinetransformidentity; } completion:^ (BOOL finished) { [email protected] ""; [Commentv Removefromsuperview]; }];}
Let's test the effect.
Well so far, keyboard calls, text boxes follow the keyboard basic implementation.
In the next section, we'll implement a text box to automatically adjust the height.
Next section address http://blog.csdn.net/lwjok2007/article/details/47403511
Apple Development Group: 414319235 Welcome to join the Welcome discussion question
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
IOS Uitextview height automatically increases with text and moves with the keyboard (one)