Previous section address: http://blog.csdn.net/lwjok2007/article/details/47401293
Then we'll implement the input box to adjust the height
First of all, we need to know whether to infer if the line is to be changed, if you want to know how much text a line can enter,
We are very easy to know the width of our input box, so we just need to know the width of each text to calculate the number of a line of text
Then we calculate the height of each line we need to add to the TextView based on the text height.
The first thing to do is to calculate the width and height of each text (we handle it as a square)
We used a method, sizewithattributes .
He gave us a way to calculate the size of the text. Detailed we can go to research API here we use directly
Create a new variable first
Float heighttext;//Text Height
The method is then used to calculate the height (provided that the. The size of the text should be 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 text box changes, and infer whether the text box width is exceeded.
Implementation of the uitextviewdelegate protocol
-(void) Textviewdidchange: (uitextview *) TextView method detects text changes
@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 corresponding number of lines of the current text if (numline>currentlinenum) {//assumes that the current text length corresponds to more than the number of lines. The height of the text box. The current view's height and position are adjusted first. Then adjust the height of the input box. Finally change the value of 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 checkWhen the number of lines decreases self.frame=cgrectmake (self.frame.origin.x, self.frame.origin.y+heighttext* (Currentlinenum-numline), 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 and see if it works out.
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.
We assume that the Limit text box is up to three lines. More than three lines of time are no longer added. Let the text box by sliding to solve, at the same time delete the text box height to reduce the minimum line
You can try.
What problems can dabigatran discuss
Code upload to group space " text box height self-adjusting 1. zip"
Apple Development Group: 414319235 Welcome to add welcome discussion questions
IOS uitextview height with text self-actively added, and trailing keyboard move (ii)