IOS custom TextView, absolutely satisfy you

We know that iOS comes with UITextView, which is hard to satisfy our daily usage. In my project, textView is inherited and UITextview, custom a TextView, the main realization of the following functions.

  1. Add placeHolder
  2. Highly adaptive
  3. You can set line spacing
  4. Support Xib

In fact, the implementation is also very simple, but very practical.
First, take a look at the document
@interface: STTextView UITextView / * * * @property placeholder text (nonatomic, copy) NSString *placeholder; / * * * / placeholder text color @property (nonatomic, strong) UIColor *placeholderColor; / / @property line spacing (nonatomic, assign) CGFloat verticalSpacing; / * * * / set the maximum height @property (nonatomic, assign) CGFloat maxHeight; / * * * set the minimum height @property (nonatomic, assign) CGFloat minHeight; / * * is not highly adaptive, the default is YES*/ @property (nonatomic, assign) BOOL isAutoHeight; @property (nonatomic, copy) void (^textDidChangedBlock) (NSString * text); @property (nonatomic, copy) void (^textViewAutoHeight) (CGFloat textHeight); @end

It should be clear at first glance.

Two. Look at the implementation process
  1. Here I’m using a UILabel as a placeholder to control its hide and display to achieve the effect. In fact, you can also draw placeHolder by overriding the draw method. Here, taking into account the actual use of the process, there will be a long placeholder, there is a line feed, the use of a Label may be more convenient, as in the following case:
IOS custom TextView, absolutely satisfy you
compatible placeholders for a long time

2. bit Label coordinate is in fact a very sophisticated
we know that the default UITextView will have a default margin for textContainerInset =UIEdgeInsetsMake (8, 0, 8, 0), so set the placeHolder coordinates should consider this point, otherwise I look like this a little obsessive people very uncomfortable. My solution is to add @property (nonatomic, assign), UIEdgeInsets, placeHolderLabelInsets, properties, and override the textContainerInset property, as follows:

#pragma mark ---setter (void) setTextContainerInset: (UIEdgeInsets) textContainerInset{/ text [super setTextContainerInset:textContainerInset] content adjustment margins; self.placeHolderLabelInsets = UIEdgeInsetsMake (textContainerInset.top, textContainerInset.bottom, textContainerInset.left + 2, textContainerInset.right + 2); [self setNeedsLayout];}

This allows you to set the margins of the text without having to adjust the coordinates of the placeHoler. Look at the actual renderings:

  1. Tv.textContainerInset = UIEdgeInsetsZero,
    , IOS custom TextView, absolutely satisfy you,
    , 1. margins are zero,.Png
  2. TextContainerInset defaults to
    IOS custom TextView, absolutely satisfy you
    and the margin is the default
  3. Set the spacing of tv.textContainerInset = UIEdgeInsetsMake (15, 10, 15, 10);
    IOS custom TextView, absolutely satisfy you
    UIEdgeInsetsMake (15, 10, 15, 10)
    that use it more easily.
    3. there is one point to note is the use of the system default keyboard, a candidate should handle the font, otherwise there will be input automatically when the selected font into the input box, experience is very bad, because the candidate text will also in the monitoring method, at this time we need to determine there is no candidate text, if (self.markedTextRange = = Nil) {/ / [self st_setAttributedString] self.textDidChangedBlock no candidate character;? Self.textDidChangedBlock (self.text): Nil;}; self.markedTextRange = = nil
    4. can be set when the spacing of delete text, we should keep the cursor position, otherwise the user to delete the intermediate font, will continue to jump to the end edge, as follows figure:
    IOS custom TextView, absolutely satisfy you
    wpap6 no cursor 03001br solve this problem is very simple only need to record the cursor position, click Reset NSRange range = self.selectedRange; self.attributedText = [[NSAttributedString alloc]initWithString:self.text attributes:[self attrs]]; self.selectedRange = range;
    IOS custom TextView, absolutely satisfy you
    cursor as shown below: basically this is to repair the
    actual use need to be aware of the problem, in fact, what are the very simple code.

Take a look at the actual effect:

IOS custom TextView, absolutely satisfy you
actual use effect

hope to help everyone, demo has uploaded to my GitHub, download, drag into the project can be used, if there are any problems in use, please contact me promptly.