IOS uitextview height with text self-actively added, and trailing keyboard move (ii)

Source: Internet
Author: User

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)

Related Article

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.