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