UITextView占位文字支持富文本

NS_ASSUME_NONNULL_BEGIN
@class LHTextView;
@protocol LHTextViewDelegate 

(void)textViewDidChange:(LHTextView *)textView;
@end
@interface LHTextView : UIView
@property (nonatomic, weak) id delegate;
@property (nonatomic, assign) UIEdgeInsets contentEdgeInsets;
@property (nonatomic, assign) CGFloat lineSpace;
@property (nonatomic, strong) UIFont *font;
@property (nonatomic, strong) UIColor *textColor;
@property (nonatomic, strong) UIColor *placeholderColor;
@property (nonatomic, strong) UIColor *tintColor;

@property (nonatomic, strong) NSString *text;
@property (nonatomic, strong) NSString *placeholder;
@property (nonatomic, strong) NSAttributedString *placeholderAttr;

@end

NS_ASSUME_NONNULL_END
import "LHTextView.h"

@interface LHTextView()
@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) UILabel *placeholderLabel;

@end
@implementation LHTextView

(NSString *)text {
return self.textView.text;
}

(void)setText:(NSString *)text {
self.textView.text = text;
if (text.length > 0) {
self.placeholderLabel.hidden = YES;
}else {
self.placeholderLabel.hidden = NO;
}
}

(void)setPlaceholderAttr:(NSAttributedString *)placeholderAttr {
_placeholderAttr = placeholderAttr;
NSMutableAttributedString *tmp = [[NSMutableAttributedString alloc] initWithAttributedString:placeholderAttr];
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.lineSpacing = _lineSpace;
NSDictionary *attributes = @{
NSForegroundColorAttributeName: _textColor,
NSParagraphStyleAttributeName: paragraphStyle
};
[tmp addAttributes:attributes range:NSMakeRange(0, placeholderAttr.length)];
self.placeholderLabel.attributedText = tmp;
}

(void)setPlaceholder:(NSString *)placeholder {
_placeholder = placeholder;
if (placeholder == nil) {
return;
}
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.lineSpacing = _lineSpace;
NSDictionary *attributes = @{ NSParagraphStyleAttributeName: paragraphStyle };
NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:placeholder attributes:attributes];
self.placeholderLabel.attributedText = attrStr;
}

(void)setTintColor:(UIColor *)tintColor {
_tintColor = tintColor;
self.textView.tintColor = tintColor;
}

(void)setFont:(UIFont *)font {
_font = font;
self.textView.font = font;
self.placeholderLabel.font = font;
}

(void)setTextColor:(UIColor *)textColor {
_textColor = textColor;
self.textView.textColor = textColor;
}

(void)setPlaceholderColor:(UIColor *)placeholderColor {
_placeholderColor = placeholderColor;
self.placeholderLabel.textColor = placeholderColor;
}

(void)setLineSpace:(CGFloat)lineSpace {
_lineSpace = lineSpace;
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.lineSpacing = _lineSpace;// 字体的行间距
UIColor *color = _textColor;
NSDictionary *attributes = @{
NSForegroundColorAttributeName: color,
NSParagraphStyleAttributeName: paragraphStyle
};
_textView.typingAttributes = attributes;

[self setPlaceholder:_placeholder];
}

(instancetype)init {
self = [super init];
if (self) {
[self setup];
}
return self;
}

(void)setup {
self.backgroundColor = [UIColor whiteColor];
self.contentEdgeInsets = UIEdgeInsetsMake(8, 8, 8, 8);
self.font = [UIFont systemFontOfSize:14];
self.textColor = [UIColor whiteColor];
self.placeholderColor = [UIColor grayColor];
self.lineSpace = 1.0;

[self addSubview:self.textView];
[self addSubview:self.placeholderLabel];

}

(void)layoutSubviews {
[super layoutSubviews];

CGFloat contentX = self.contentEdgeInsets.left;
CGFloat contentY = self.contentEdgeInsets.top;
CGFloat contentW = self.bounds.size.width - self.contentEdgeInsets.left - self.contentEdgeInsets.right;
CGFloat contentH = self.bounds.size.height - self.contentEdgeInsets.top - self.contentEdgeInsets.bottom;
self.textView.frame = CGRectMake(contentX, contentY, contentW, contentH);

self.placeholderLabel.frame = CGRectMake(contentX, contentY, contentW, 0);
[self.placeholderLabel sizeToFit];
}

pragma mark - UITextViewDelegate

(void)textViewDidChange:(UITextView *)textView {
if (!textView.text.length) {
self.placeholderLabel.alpha = 1;
} else {
self.placeholderLabel.alpha = 0;
}
if ([_delegate respondsToSelector:@selector(textViewDidChange:)]) {
[_delegate textViewDidChange:self];
}
}
pragma mark - lazy

(UILabel *)placeholderLabel {
if (!_placeholderLabel) {
_placeholderLabel = [[UILabel alloc] init];
_placeholderLabel.numberOfLines = 0;
_placeholderLabel.backgroundColor = [UIColor clearColor];

}
return _placeholderLabel;
}

(UITextView *)textView {
if (!_textView) {
_textView = [[UITextView alloc] init];
_textView.backgroundColor = [UIColor clearColor];
_textView.delegate = self;
_textView.textContainerInset = UIEdgeInsetsZero;
_textView.textContainer.lineFragmentPadding = 0;
}
return _textView;
}
@end

你可能感兴趣的:(UITextView占位文字支持富文本)