iOS Settings TextView placeholder

Source: Internet
Author: User

In iOS, the general TextField can display a navigation text, there is placeholder (placeholder, used to display the message), TextView can display multiple lines of text but there is no placeholder this, the following method can solve the problem.

@interface Personsetviewcontroller () <UITextViewDelegate>

Compliance with agency agreements

-(void) Viewdidload {

[Super Viewdidload];

Two TextView to display placeholder at the bottom, set them to non-editable

[_farmnametextview Seteditable:no];

[_farmdiscribetextview Seteditable:no];

Two TextView for user input on top, set self as proxy

Self.farmNameTView.delegate =self;

Self.farmDiscrebeTView.delegate =self;

}

The method used to implement

-(BOOL) TextView: (Uitextview *) TextView Shouldchangetextinrange: (nsrange) Range Replacementtext: (NSString *) text{

if (![ Text isequaltostring:@ ""])//If text is not empty, then hide the following two TextView

{

[_farmnametextview Sethidden:yes];

[_farmdiscribetextview Sethidden:yes];

}

If the character is empty and the cursor is in the first position, and there are no characters, then the following two TextView are displayed

if ([Text isequaltostring:@ ""] && range.length==1 && range.location==0) {

[_farmnametextview Sethidden:no];

[_farmdiscrebetview Sethidden:no];

}

If you encounter \ n (enter), then close the keyboard

if ([text isequaltostring:@ "\ n"]) {

[TextView Resignfirstresponder];

return NO; Status is not editable

}

return YES; Status for Edit

}

When the keyboard appears, you need to move the original view upward, with animation to achieve the effect

Start editing

-(void) textviewdidbeginediting: (Uitextview *) textview{

[UIView Beginanimations:nil Context:nil]; Animation start

[UIView setanimationduration:0.2];//animation duration 0.2 sec.

[UIView Setanimationbeginsfromcurrentstate:yes]; Set the animation to start from the current state

Self.datascrollview.frame=cgrectmake (0, 0, cgrectgetwidth (self.dataScrollView.frame), Cgrectgetheight ( Self.dataScrollView.frame)); Set the frame of the view in edit state

[UIView commitanimations]; Submit Animation

}

End Edit

-(void) textviewdidendediting: (Uitextview *) textview{

[UIView Beginanimations:nil Context:nil];

[UIView setanimationduration:0.2];

[UIView Setanimationbeginsfromcurrentstate:yes];

Self.datascrollview.frame=cgrectmake (0, 133, Cgrectgetwidth (Self.dataScrollView.frame), Cgrectgetheight ( Self.dataScrollView.frame)); Set the frame of the view in a non-editable state

[UIView commitanimations];

}

iOS Settings TextView placeholder

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.