由于项目中许多地方用到textView,并且要有placeholder和自动计数、字数限制,所以自己就想着写一个,可以方便很多地方调用。
演示.gif1.先新建一个继承UITextView的文件,主要是在这里实现placeholder、clearButton以及自动计数控件的通用类设置,还有一些代理方法:
@property (nonatomic, strong) NSString *placeholder;
@property (nonatomic, strong) UIColor *placeholderColor;
@property (nonatomic, strong) UIFont *placeholderFont;
//允许输入的最大长度
@property (nonatomic, assign) NSInteger maxLength;
//是否显示 计数器 label
@property (nonatomic, assign) BOOL showWordCountLabel;
-(void)setPlaceholder:(NSString *)placeholder
{
_placeholder = placeholder;
[self setNeedsDisplay];
}
-(void)setPlaceholderFont:(UIFont *)placeholderFont
{
_placeholderFont = placeholderFont;
[self setNeedsDisplay];
}
-(void)setPlaceholderColor:(UIColor *)placeholderColor
{
_placeholderColor = placeholderColor;
[self setNeedsDisplay];
}
2.建一个继承UIView的文件,在这里设置属性和回调方法,把上面创建的头文件导入进来,如下方法:
-(void)textViewDidChange:(UITextView *)textView
{
[self textViewTextLengthChange:textView.text.length];
self.maxTextCount = 60;
NSString *toBeString = textView.text;
NSString *lang = [(UITextInputMode*)[[UITextInputMode activeInputModes] firstObject] primaryLanguage]; // 键盘输入模式
if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入,包括简体拼音,健体五笔,简体手写
UITextRange *selectedRange = [textView markedTextRange];
//获取高亮部分
UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
// 没有高亮选择的字,则对已输入的文字进行字数统计和限制
if (!position) {
if (toBeString.length >= self.maxTextCount) {
textView.text = [toBeString substringToIndex:self.maxTextCount];
}
self.countLabel.text=[NSString stringWithFormat:@"(%lu/%@)",(unsigned long)_textView.text.length, @(self.maxTextCount)];
[self changeTextWithTextColor:[UIColor orangeColor] OfLabel:self.countLabel withLocation:1 andLength:self.countLabel.text.length-5];
} // 有高亮选择的字符串,则暂不对文字进行统计和限制
else{
}
}
// 中文输入法以外的直接对其统计限制即可,不考虑其他语种情况
else{
if (toBeString.length >= self.maxTextCount) {
textView.text = [toBeString substringToIndex:self.maxTextCount];
}
self.countLabel.text=[NSString stringWithFormat:@"(%lu/%@)",(unsigned long)_textView.text.length, @(self.maxTextCount)];
[self changeTextWithTextColor:[UIColor orangeColor] OfLabel:self.countLabel withLocation:1 andLength:self.countLabel.text.length-5];
}
if ([_delegate respondsToSelector:@selector(textViewDidChange:)]) {
[_delegate textViewDidChange:textView];
}
}
- 这里进行了一个高亮判断和输入法的判断,主要是在计数的时候,能够在选中高亮文字之后再执行,这样就不会造成输入字数达到最大限制时计数label显示不正确,或者是键盘直接不能用了,达到的效果就是字数达到限制时,键盘还能输入,textView不再输入或高亮选中无效
3.在需要的地方调用就可以了
//textView
LALCustomTextView *customTextView =[[NSBundle mainBundle] loadNibNamed:@"LALCustomTextView" owner:self options:nil].lastObject;
[self.view addSubview:customTextView];
customTextView.delegate = self;
customTextView.clearButtonType = ClearButtonAppearWhenEditing;
[customTextView setPlaceholder:@"编辑新通知,60字以内..." contentText:_inputText maxTextCount:60];
__weak typeof (self) weakSelf = self;
customTextView.frame = CGRectMake(weakSelf.view.frame.origin.x, 0, weakSelf.view.frame.size.width, 200);
具体代码可直接去下载,有错误或改进的地方,请直接@我,不吝赐教。
代码传送门:demo
网友评论