typedef NS_ENUM(NSInteger, YKTextViewMode) {
YKTextViewModeNever,
YKTextViewModeWhileEditing,
};
#import <UIKit/UIKit.h>
@interface YKTextView : UITextView
@property (nonatomic, strong) NSString *placehoder; // 占位文字
@property (nonatomic, strong) UIColor *placehoderColor; // 占位文字颜色
@property (nonatomic, assign) BOOL showPlacehoder; // 输入后是否显示占位文字
@property (nonatomic, assign) YKTextViewMode clearButtonMode;
@end
#import "YKTextView.h"
@interface YKTextView ()<UITextViewDelegate>
@property (nonatomic, strong) UILabel *placehoderLabel;
@property (nonatomic, strong) UIButton *clearButton;
@end
@implementation YKTextView
- (UIButton *)clearButton {
if (!_clearButton) {
_clearButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_clearButton setBackgroundImage:[UIImage imageNamed:@"clearImage"] forState:UIControlStateNormal];
_clearButton.backgroundColor = [UIColor clearColor];
_clearButton.hidden = YES;
[_clearButton addTarget:self action:@selector(clearAllText) forControlEvents:UIControlEventTouchUpInside];
}
return _clearButton;
}
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor clearColor];
UILabel *placehoderLabel = [[UILabel alloc] init];
placehoderLabel.numberOfLines = 0;
placehoderLabel.backgroundColor = [UIColor clearColor];
[self addSubview:placehoderLabel];
self.placehoderLabel = placehoderLabel;
self.placehoderColor = [UIColor lightGrayColor];
self.font = kFont(17);
self.placehoderLabel.font = kFont(17);
self.clearButton.frame = CGRectMake(self.size.width - 17.5, self.size.height - 17.5, 15, 15);
[self addSubview:self.clearButton];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self];
}
return self;
}
- (void)clearAllText {
self.text = @"";
}
- (void)setClearButtonMode:(YKTextViewMode)clearButtonMode {
_clearButtonMode = clearButtonMode;
}
- (void)textDidChange {
if (self.clearButtonMode == YKTextViewModeNever) {
self.clearButton.hidden = YES;
} else {
self.clearButton.hidden = NO;
}
if (!self.showPlacehoder) {
self.placehoderLabel.hidden = YES;
} else {
self.placehoderLabel.hidden = NO;
}
}
- (void)textViewDidChange:(UITextView *)textView {
}
- (void)setText:(NSString *)text {
[super setText:text];
[self textDidChange];
}
- (void)setAttributedText:(NSAttributedString *)attributedText {
[super setAttributedText:attributedText];
[self textDidChange];
}
- (void)setPlacehoder:(NSString *)placehoder {
_placehoder = placehoder;
self.placehoderLabel.text = placehoder;
[self setNeedsLayout];
}
- (void)layoutSubviews {
[super layoutSubviews];
self.placehoderLabel.mj_x = 15,
self.placehoderLabel.mj_y = 10;
self.placehoderLabel.width = self.width - 2 * self.placehoderLabel.mj_x;
CGFloat height = [self.placehoder boundingSize:CGSizeMake(self.placehoderLabel.width, MAXFLOAT) attributes:@{NSFontAttributeName : kFont(17), NSForegroundColorAttributeName : self.placehoderColor}];
self.placehoderLabel.height = height;
}
// 此方法在为重写textViewDidChange进行首行缩进
#pragma mark - 设置首行缩进
-(void)textViewDidChange:(UITextView *)textView{
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 4; //行间距
paragraphStyle.maximumLineHeight = 60; /**最大行高*/
CGFloat width = [self.myTextView.placehoder boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : kFont(16), NSForegroundColorAttributeName : RGBColor(51, 51, 51)} context:nil].size.width;
paragraphStyle.firstLineHeadIndent = width + 10; /**首行缩进宽度*/
paragraphStyle.alignment = NSTextAlignmentJustified;
NSDictionary *attributes = @{
NSFontAttributeName:[UIFont systemFontOfSize:16],
NSParagraphStyleAttributeName:paragraphStyle
};
textView.attributedText = [[NSAttributedString alloc] initWithString:textView.text attributes:attributes];
}