由于項(xiàng)目中許多地方用到textView,并且要有placeholder和自動(dòng)計(jì)數(shù)放闺、字?jǐn)?shù)限制祟昭,所以自己就想著寫一個(gè),可以方便很多地方調(diào)用怖侦。
演示.gif
1.先新建一個(gè)繼承UITextView的文件篡悟,主要是在這里實(shí)現(xiàn)placeholder、clearButton以及自動(dòng)計(jì)數(shù)控件的通用類設(shè)置匾寝,還有一些代理方法:
@property (nonatomic, strong) NSString *placeholder;
@property (nonatomic, strong) UIColor *placeholderColor;
@property (nonatomic, strong) UIFont *placeholderFont;
//允許輸入的最大長(zhǎng)度
@property (nonatomic, assign) NSInteger maxLength;
//是否顯示 計(jì)數(shù)器 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.建一個(gè)繼承UIView的文件搬葬,在這里設(shè)置屬性和回調(diào)方法,把上面創(chuàng)建的頭文件導(dǎo)入進(jìn)來艳悔,如下方法:
-(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"]) { // 簡(jiǎn)體中文輸入急凰,包括簡(jiǎn)體拼音,健體五筆很钓,簡(jiǎn)體手寫
UITextRange *selectedRange = [textView markedTextRange];
//獲取高亮部分
UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
// 沒有高亮選擇的字香府,則對(duì)已輸入的文字進(jìn)行字?jǐn)?shù)統(tǒng)計(jì)和限制
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];
} // 有高亮選擇的字符串,則暫不對(duì)文字進(jìn)行統(tǒng)計(jì)和限制
else{
}
}
// 中文輸入法以外的直接對(duì)其統(tǒng)計(jì)限制即可码倦,不考慮其他語種情況
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];
}
}
- 這里進(jìn)行了一個(gè)高亮判斷和輸入法的判斷企孩,主要是在計(jì)數(shù)的時(shí)候,能夠在選中高亮文字之后再執(zhí)行袁稽,這樣就不會(huì)造成輸入字?jǐn)?shù)達(dá)到最大限制時(shí)計(jì)數(shù)label顯示不正確勿璃,或者是鍵盤直接不能用了,達(dá)到的效果就是字?jǐn)?shù)達(dá)到限制時(shí),鍵盤還能輸入推汽,textView不再輸入或高亮選中無效
3.在需要的地方調(diào)用就可以了
//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字以內(nèi)..." contentText:_inputText maxTextCount:60];
__weak typeof (self) weakSelf = self;
customTextView.frame = CGRectMake(weakSelf.view.frame.origin.x, 0, weakSelf.view.frame.size.width, 200);
具體代碼可直接去下載补疑,有錯(cuò)誤或改進(jìn)的地方,請(qǐng)直接@我歹撒,不吝賜教莲组。
代碼傳送門:demo