We all know that UITextField has the placeholder attribute, and UITextView does not have placeholder. How can we simulate UITextfield to make UITextView have placeholder. The idea is: Inherit the uitextview and make [super text] display placeholder when the text is null. The Code is as follows (I referenced arc): UIPlaceholderTextView. h
# Import <UIKit/UIKit. h> @ interface UIPlaceholderTextView: UITextView @ property (nonatomic, strong) NSString * placeholder; // placeholder-(void) addObserver; // Add notification-(void) removeobserver; // remove notification @ end # import <UIKit/UIKit. h> @ interface UIPlaceholderTextView: UITextView @ property (nonatomic, strong) NSString * placeholder; // placeholder-(void) addObserver; // Add notification-(void) removeobserver; // remove notification @ endUIPlaceholderTextView. m
# Import "UIPlaceholderTextView. h "@ interface UIPlaceholderTextView () @ property (nonatomic, strong) UIColor * textColor;-(void) beginEditing :( NSNotification *) notification;-(void) endEditing :( NSNotification *) notification; @ end @ implementation UIPlaceholderTextView @ synthesize placeholder; @ synthesize textColor;-(id) initWithFrame :( CGRect) frame {if (self = [super initWithFrame: frame]) {[self awakeFromNib];} return self;} // this method is called when a nib is created-(void) awakeFromNib {textColor = [UIColor redColor]; [self addObserver];}-(void) addObserver {[[nsicationcenter center defacenter center] addObserver: self selector: @ selector (beginEditing :) name: UITextViewTextDidBeginEditingNotification object: self]; [[nsicationcenter center defacenter center] addObserver: self selector: @ selector (endEditing :) name: jsonobject: self];}-(void) removeobserver {[[nsicationicationcenter defacenter center] removeObserver: self] ;}# pragma mark-# pragma mark Setter/Getters-(void) setPlaceholder :( NSString *) aPlaceholder {placeholder = aPlaceholder; [self endEditing: nil];} -(NSString *) text {NSString * text = [super text]; if ([text is1_tostring: placeholder]) return @ ""; return text;}-(void) beginEditing :( NSNotification *) notification {if ([super. text isw.tostring: placeholder]) {super. text = nil; // font color [super setTextColor: textColor] ;}}- (void) endEditing :( NSNotification *) notification {if ([super. text isEqualToString: @ ""] | self. text = nil) {super. text = placeholder; // annotation color [super setTextColor: [UIColor lightGrayColor] ;}# import "UIPlaceholderTextView. h "@ interface UIPlaceholderTextView () @ property (nonatomic, strong) UIColor * textColor;-(void) beginEditing :( NSNotification *) notification;-(void) endEditing :( NSNotification *) notification; @ end @ implementation UIPlaceholderTextView @ synthesize placeholder; @ synthesize textColor;-(id) initWithFrame :( CGRect) frame {if (self = [super initWithFrame: frame]) {[self awakeFromNib];} return self;} // this method is called when a nib is created-(void) awakeFromNib {textColor = [UIColor redColor]; [self addObserver];}-(void) addObserver {[[nsicationcenter center defacenter center] addObserver: self selector: @ selector (beginEditing :) name: UITextViewTextDidBeginEditingNotification object: self]; [[nsicationcenter center defacenter center] addObserver: self selector: @ selector (endEditing :) name: jsonobject: self];}-(void) removeobserver {[[nsicationicationcenter defacenter center] removeObserver: self] ;}# pragma mark-# pragma mark Setter/Getters-(void) setPlaceholder :( NSString *) aPlaceholder {placeholder = aPlaceholder; [self endEditing: nil];} -(NSString *) text {NSString * text = [super text]; if ([text is1_tostring: placeholder]) return @ ""; return text;}-(void) beginEditing :( NSNotification *) notification {if ([super. text isw.tostring: placeholder]) {super. text = nil; // font color [super setTextColor: textColor] ;}}- (void) endEditing :( NSNotification *) notification {if ([super. text isEqualToString: @ ""] | self. text = nil) {super. text = placeholder; // annotation color [super setTextColor: [UIColor lightGrayColor];}
When using ViewController, 1. Add a uitextview connection to xib; 2. directly create initframe, but do not forget, placeholder, and add and remove notifications.
-(Void) viewDidLoad {[super viewDidLoad]; self. textView. placeholder = @ "Please add your information ...... "; // Do any additional setup after loading the view, typically from a nib .} -(void) viewDidAppear :( BOOL) animated {[super viewDidAppear: YES]; [self. textView addObserver];}-(void) viewDidDisappear :( BOOL) animated {[super viewDidDisappear: YES]; [self. textView removeobserver];}-(void) viewDidLoad {[super viewDidLoad]; self. textView. placeholder = @ "Please add your information ...... "; // Do any additional setup after loading the view, typically from a nib .} -(void) viewDidAppear :( BOOL) animated {[super viewDidAppear: YES]; [self. textView addObserver];}-(void) viewDidDisappear :( BOOL) animated {[super viewDidDisappear: YES]; [self. textView removeobserver];}
: