One line of code implements UITextView’s placeHolder

One line of code implements UITextView's placeHolder
reading useless theory of death.Jpg


Implement UITextView’s placeHolder through a line of Category. Collocation uses one line of code to restrict and count UITextView.

Implementation effect

One line of code implements UITextView's placeHolder

Import header file

#import "UITextView+ZWPlaceHolder.h""
  • The storyboard (Xib) line calls the code “self.firstTextView.zw_placeHolder = @” to feed the industry related activities, product information, and to market analysis. “;
  • Call the CGRect code to create UITextView rect = CGRectMake (5, 230, [UIScreen mainScreen].bounds.size.width-10, 80); UITextView *textView = [[UITextView alloc] initWithFrame:rect]; textView.layer.borderWidth = 1; textView.font = [UIFont systemFontOfSize:14]; textView.layer.borderColor [UIColor = lightGrayColor].CGColor; textView.zw_placeHolder = @ “feedback to the manufacturers, product information, industry related activities for market analysis. “[self.view addSubview:textView];
  • Adjust the font size placeHolder placeholder font size will follow the UITextView font size changes, can be adjusted by UITextView font to adjust the font size PlaceHolder textView.font = [UIFont systemFontOfSize:14];
  • Adjust the font color of textView.zw_placeHolderColor = [UIColor = redColor]; One line of code implements UITextView's placeHolder
    , adjust the placeHolder font color.Png, placeHolder source code analysis
  • In the UITextView category runtime zw_placeHolder by adding attributes, when the assignment to zw_placeHolder, to create a UILabel add to UITextView, then text UITextView in monitoring changes to choose whether or not to display placeHolder.
  • Kernel code
#pragma mark - Update - (void) updatePlaceHolder{if (self.text.length) {[self.zw_placeHolderLabel} removeFromSuperview]; return; self.zw_placeHolderLabel.font = self.font? Self.font:self.cacutDefaultFont; self.zw_placeHolderLabel.textAlignment = self.textAlignment; self.zw_placeHolderLabel.text = self.zw_placeHolder; [self insertSubview:self.zw_placeHolderLabel atIndex:0];} #pragma - lazzing - mark (UILabel * zw_placeHolderLabel{) UILabel * placeHolderLab = objc_getAssociatedObject (self, @selector (zw_placeHolderLabel)); if (placeHolderLab! = [[UILabel) {placeHolderLab alloc] init]; placeHolderLab.numberOfLines = 0; placeHolderLab.textColor = [UIColor lightGrayColor]; Objc_setAssociatedObject (self, @selector (zw_placeHolderLabel), placeHolderLab, OBJC_ASSOCIATION_RETAIN); [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (updatePlaceHolder) name: UITextViewTextDidChangeNotification object:self] return placeHolderLab;}};
  • How to prevent placeHolder from being blocked by cursor. The default margin for
    UITextView cursor is 5. Value: 5 The layout / / Default padding at the beginning and end of the line fragment rects insetting the layout width available for the contents. This value is utilized by NSLayoutManager for determining the layout width. @property (NS_NONATOMIC_IOSONLY) CGFloat lineFragmentPadding; so the calculation of custom placeHolder position need to add the margin
  • The textContainerInset on UITextView causes text offset, and UITextView can offset the text input region by setting the textContainerInset. CGFloat x = lineFragmentPadding + textContainerInset.left + self.layer.borderWidth; placeHolder’s X coordinate = cursor offset +text offset +border border width.
    other coordinates are equally calculated.

How to use

  • Cocoapods import (search failed, please update local warehouse) pod’ZWPlaceHolder’
  • Directly drag the file into the project and introduce the header file to #import “UITextView+ ZWPlaceHolder.h””


  • Refer to the last line of code, restrict and count UITextView, input words,
    , One line of code implements UITextView's placeHolder,
    , and.Gif

Source code

  • Source code on the GitHub, please correct me, remember star Oh!