IOS settings TextView with placeholder text

Source: Internet
Author: User

Native TextView no placeholder text, can be DrawRect: method to add placeholder text, the specific settings are as follows:

1. Get the current placeholder Text property:

// Text Properties Nsmutabledictionary *attrs =//  Gets the Text property of the current Placeholdertextview attrs[ Nsforegroundcolorattributename] = Self.placeholdercolor? Self.placeholdercolor: [Uicolor Lightgraycolor];

2. Set the drawing range (including text margins):

//Drawing RangeCGFloat placeholdertopmargin = Self.placeholdertopmargin?Self.placeholderTopMargin:kPlaceholderDefaultTopMargin; CGFloat Placeholderleftmargin= Self.placeholderleftmargin?Self.placeholderLeftMargin:kPlaceholderDefaultTopMargin; CGFloat Placeholderx=Placeholdertopmargin; CGFloat Placeholdery=Placeholderleftmargin; CGFloat Placeholderw= Rect.size.width-2*Placeholderleftmargin; CGFloat Placeholderh= Rect.size.height-2*Placeholdertopmargin; CGRect Placeholderrect=CGRectMake (Placeholderx, Placeholdery, Placeholderw, Placeholderh); [Self.placeholder drawinrect:placeholderrect withattributes:attrs];

3. By notification or agent, when the TextView text changes, redraw:

// set up notifications to send to yourself when TextView text changes object: Self];

Notification events:

-(void) textdidchanged{    //  redraw     [self setneedsdisplay];}

4. Override the TextView property method to draw in real time:

-(void) Setplaceholder: (NSString *) placeholder{    = [placeholder copy];         // will call DrawRect in the next message loop     [self setneedsdisplay];}

-(void) Setplaceholdercolor: (Uicolor *) placeholdercolor{    = placeholdercolor;        [Self setneedsdisplay];}

-(void) SetText: (NSString *) text{    //  system comes with properties         [self setneedsdisplay] ;}

-(void) Setattributedtext: (nsattributedstring *) attributedtext{    [Super Setattributedtext: Attributedtext];        [Self setneedsdisplay];}

-(void) SetFont: (Uifont *) font{    [Super Setfont:font];        [Self setneedsdisplay];}

Githubs:https://github.com/bigplane/chplaceholdertextview

IOS settings TextView with placeholder text

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.