iOS UITextView限制输入字数

//限制字数100字
-(void)textViewDidChange:(UITextView *)textView {
    UITextRange *selectedRange = [textView markedTextRange];
    //获取高亮部分
    UITextPosition *pos = [textView positionFromPosition:selectedRange.start offset:0];
   //如果在变化中是高亮部分在变,就不要计算字符了
    if (selectedRange && pos) {
        return;
    }
    if (textView.text.length > 100) {
        textView.text = [textView.text substringToIndex:100];
    }else {
        self.numberLb.textColor = JHFontColor2;
    }
    self.numberLb.text = [NSString stringWithFormat:@"%zd/100",textView.text.length];
}

你可能感兴趣的:(iOS UITextView限制输入字数)