Previous section address: http://blog.csdn.net/lwjok2007/article/details/47401293
And then we're going to implement the automatic height adjustment of the input box.
First of all, we need to know, to determine whether to change the line is the premise is to know how much text line can be entered,
We can easily know the width of our input box, so we can calculate the number of lines of text as long as we know the width of each text.
Then the height of each line we need to add to the TextView is calculated based on the text height.
The first thing to do is to calculate the width and height of each text (we deal with the square)
We used a method, sizewithattributes .
He gives us a way to calculate the size of the text, we can go to research API here we use directly
Create a new variable first
Float heighttext;//Text Height
Then use this method to calculate the height (provided that the size of the text is known in advance, we will calculate according to 20)
Nsdictionary *[email Protected]{nsfontattributename:[uifont systemfontofsize:20.0]}; Cgsize contentsize=[@ "i" sizewithattributes:dict]; Heighttext=contentsize.height;
Next we need to calculate the length of all text when the contents of the text box change, to determine whether the text box width is exceeded
Implementation of the uitextviewdelegate protocol
-(void) Textviewdidchange: (uitextview *) TextView method Detect text modification
@interface Commentview () <UITextViewDelegate>
-(void) Textviewdidchange: (Uitextview *) textview{float currentlinenum=1;//default text box displays a line of text float Textviewwidth=self.text view.frame.size.width;//Get text box height NSString *content=textview.text; Nsdictionary *[email protected]{nsfontattributename:[uifont systemfontofsize:20.0]}; Cgsize contentsize=[content sizewithattributes:dict];//Calculating text length float NUMLINE=CEILF (contentsize.width/textviewwidth) ; Calculates the number of lines corresponding to the current text if (numline>currentlinenum) {//If the current text length is found to correspond to more than the number of lines. text box height, adjust the height and position of the current view, adjust the height of the input box, and finally modify the value of the Currentlinenum self.frame=cgrectmake (self.frame.origin.x, self.frame.origin.y-heighttext* (Numline-currentlinenum), Self.frame.size.width, self.frame.size.height+ heighttext* (Numline-currentlinenum)); Textview.frame=cgrectmake (textview.frame.origin.x, TEXTVIEW.FRAME.ORIGIN.Y, TextView.frame.size.width, textview.frame.size.height+heighttext* (Numline-currentlinenum)); Currentlinenum=numline; }else if (numline<currentlinenum) {//number of times is deleted when the checkSelf.frame=cgrectmake (self.frame.origin.x, self.frame.origin.y+heighttext* (currentlinenum-numline) when the number of lines of text is reduced, Self.frame.size.width, self.frame.size.height-heighttext* (Currentlinenum-numline)); Textview.frame=cgrectmake (textview.frame.origin.x, TEXTVIEW.FRAME.ORIGIN.Y, TextView.frame.size.width, textview.frame.size.height-heighttext* (Currentlinenum-numline)); Currentlinenum=numline; }}
All right, let's try the test, see if it works.
Finally, give us a little bit of a problem, try to solve it.
Let's say we make comments. The view line number cannot grow indefinitely or exceed the screen.
If we limit the text box up to three lines, more than three lines of time will not increase, so that the text box by sliding to solve, while deleting the text box to reduce the height of the minimum line
We can try.
What's wrong with the group discussion?
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 follows keyboard movement (ii)