Placeholder of iphone custom UITextView

Source: Internet
Author: User

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];}

 

:

Related Article

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.