In iOS we Uitextfield have placeholder function, but Uitextview does not have this method, sometimes we also need to implement such placeholder function, StackOverflow gives us a way to rewrite the Uitextview, a small amount of code to achieve this function, it is worth learning.
#import <UIKit/UIKit.h> @interface Uiplaceholdertextview:uitextview@property (nonatomic, retain) NSString * placeholder; @property (nonatomic, retain) Uicolor *placeholdercolor;-(void) textChanged: (nsnotification*) notification; @end
#import "UIPlaceHolderTextView.h" @interface Uiplaceholdertextview () @property (nonatomic, retain) UILabel * Placeholderlabel, @end @implementation uiplaceholdertextviewcgfloat const Ui_placeholder_text_changed_animation_ DURATION = 0.25;-(void) dealloc{[[Nsnotificationcenter Defaultcenter] removeobserver:self]; #if __has_feature (objc_ ARC) #else [_placeholderlabel release]; _placeholderlabel = nil; [_placeholdercolor release]; _placeholdercolor = nil; [_placeholder release]; _placeholder = nil; [Super Dealloc]; #endif}-(void) awakefromnib{[Super awakefromnib]; Use Interface Builder User Defined Runtime Attributes to set//placeholder and placeholdercolor in Interface Builde R. if (!self.placeholder) {[Self setplaceholder:@ "]; } if (!self.placeholdercolor) {[self setplaceholdercolor:[uicolor lightgraycolor]]; } [[Nsnotificationcenter defaultcenter] addobserver:self selector: @selector (textChanged:) Name:uitextviewtextdidchaNgenotification Object:nil];} -(ID) initWithFrame: (CGRect) frame{if (self = [super Initwithframe:frame])} {[Self setplaceholder:@ "]; [Self Setplaceholdercolor:[uicolor lightgraycolor]]; [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (textChanged:) Name: Uitextviewtextdidchangenotification Object:nil]; } return self;} -(void) textChanged: (nsnotification *) notification{if ([[Self placeholder] length] = = 0) {return; } [UIView animatewithduration:ui_placeholder_text_changed_animation_duration animations:^{if ([[Self TEXT] L Ength] = = 0) {[[Self viewwithtag:999] setalpha:1]; } else {[self viewwithtag:999] setalpha:0]; } }];} -(void) SetText: (NSString *) text {[Super Settext:text]; [Self textchanged:nil];} -(void) DrawRect: (CGRect) rect{if ([[Self placeholder] length] > 0) {if (_placeholderlabel = = nil) { _placeholderlabel = [[UILabel alloc] Initwithframe:cgrectmake (8,8,self.bounds.size.width-16,0)]; _placeholderlabel.linebreakmode = nslinebreakbywordwrapping; _placeholderlabel.numberoflines = 0; _placeholderlabel.font = Self.font; _placeholderlabel.backgroundcolor = [Uicolor Clearcolor]; _placeholderlabel.textcolor = Self.placeholdercolor; _placeholderlabel.alpha = 0; _placeholderlabel.tag = 999; [Self Addsubview:_placeholderlabel]; } _placeholderlabel.text = Self.placeholder; [_placeholderlabel SizeToFit]; [Self Sendsubviewtoback:_placeholderlabel]; if ([self text] length] = = 0 && [[self placeholder] length] > 0) {[[Self viewwithtag:999] SETALPHA:1]; } [Super Drawrect:rect];} @end
Uitextview implementation of placeholder functions