UITextView- sets placeholder placeholder text

—–> in development, with the increase in input text, Textfile has been unable to meet our needs, and it can not line function, but also let us worry about it. We only choose UITextView, but not UITextView (placeholder) for the function of the text, it is not both fish and bear’s paw. Now let’s do it:

  • We customize a UITextView WBPlaceholderTextView.h file #import < UIKit/UIKit.h> @interface: WBPlaceholderTextView UITextView / * * * @property placeholder text (nonatomic, copy) NSString *placeholder; / * * * / booth text color @property (nonatomic, strong) UIColor *placeholderColor; @end

The WBPlaceholderTextView.m file #import “WBPlaceholderTextView.h” @implementation WBPlaceholderTextView (instancetype) – initWithFrame: (CGRect frame) {self = [super initWithFrame:frame]; if (self) {/ / monitoring text input (textDidChange) “NSNotificationCenter defaultCenter]addObserver:self selector:@selector name:UITextViewTextDidChangeNotification object:self] return self;};} – (void) setPlaceholder: (NSString *) placeholder{_placeholder = [placeholder copy]; [self setNeedsDisplay];} – (void) setPlaceholderColor: (UIColor * placeholderColor{) _placeholderColor = placeholderColor; [self setNeedsDisplay];} – (void) setFont: (UIFont *) font{[super setFont:font]; [self setNeedsDisplay];} – (V OID) setText: (NSString *) text{[super setText:text]; [self setNeedsDisplay];} – (void) setAttributedText: (NSAttributedString *) attributedText{[super setAttributedText:attributedText]; [self setNeedsDisplay];} – (void) textDidChange{/ redraw [self setNeedsDisplay];} – (void) drawRect: (CGRect rect) {/ / if the text box value returns if (self.hasText return CGFloat X); CGFloat = 5; Y = 8; CGFloat W = rect.size.width – 2 * X; CGFloat H = rect.size.width – 2 * Y; NSMutableDictionary *attrt [NSMutableDictionary = dictionary]; attrt[NSForegroundColorAttributeName] = self.placeholderColor? Self.placeholderColor:[UIColor grayColor]; attrt[NSFontAttributeName] self.font:[UIFont sy = self.font? StemFontOfSize:17.0]; [self.placeholder drawInRect:CGRectMake (X, Y, W, H, withAttributes:attrt]);} / / removal notice, to prevent wild pointer – (void) dealloc{[[NSNotificationCenter defaultCenter]removeObserver:self] @end;}

At this point, a Textview with placeholder placeholder text has been completed, and we like to add a bit of attention, and we discuss it together

Contact me, QQ:2258026527