项目遇到一个需求就是显示输入的字数还有多少可用,如图所示
准备工作:
1.添加UITextViewDelegate代理,使用UITextView代理方法即可实现,
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
以及-(void)textViewDidChange:(UITextView *)textView
2.创建一个UIlabel对象,作为显示剩余可输入字数/最大字数限制
UILabel *textNumLabel;
代码如下:
//最大输入字数宏`
#define MAX_LIMIT_NUMS 10
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
//不支持系统表情输入
if ([[textView textInputMode] primaryLanguage] == nil || [[[textView textInputMode] primaryLanguage] isEqualToString:@"emoji"]) {
return NO;
}
UITextRange *selectedRange = [textView markedTextRange];
UITextPosition *pos = [textView positionFromPosition:selectedRange.start offset:0];
if (selectedRange && pos) {
NSInteger startOffset = [textView offsetFromPosition:textView.beginningOfDocument toPosition:selectedRange.start];
NSInteger endOffset = [textView offsetFromPosition:textView.beginningOfDocument toPosition:selectedRange.end];
NSRange offsetRange = NSMakeRange(startOffset, endOffset - startOffset);
if (offsetRange.location < MAX_LIMIT_NUMS) {
return YES;
} else {
return NO;
}
}
NSString *comcatStr = [textView.text stringByReplacingCharactersInRange:range withString:text];
NSInteger canInputLen = MAX_LIMIT_NUMS - comcatStr.length;
if (canInputLen >= 0) {
return YES;
} else {
NSInteger len = text.length + canInputLen;
//防止当text.length + canInputLen < 0时,使得rg.length为一个非法最大正数出错
NSRange rg = {0,MAX(len, 0)};
if (rg.length > 0) {
NSString *str = @"";
//判断是否只普通的字符或ASCII码
BOOL asc = [text canBeConvertedToEncoding:NSASCIIStringEncoding];
if (asc) {
str = [text substringWithRange:rg];
} else {
__block NSInteger idx = 0;
__block NSString *trimString = @"";//截取出的字符
//使用字符串遍历,这个方法能准确的知道每个emoji是占一个Unicode还是两个
[text enumerateSubstringsInRange:NSMakeRange(0, [text length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString * _Nullable substring, NSRange substringRange, NSRange enclosingRange, BOOL * _Nonnull stop) {
if (idx >= rg.length) {
*stop = YES;
return;
}
trimString = [trimString stringByAppendingString:substring];
idx++;
}];
str = trimString;
}
//rang是指从当前光标处进行替换处理,注意如果执行此句后面返回的是yes会触发didchange事件
[textView setText:[textView.text stringByReplacingCharactersInRange:range withString:str]];
//既然是超出去部分截取了,那一定是最大限制了
textNumLabel.text = [NSString stringWithFormat:@"%d/%ld",0,(long)MAX_LIMIT_NUMS];
}
return NO;
}
}
//显示当前可输入字数/总字数
-(void)textViewDidChange:(UITextView *)textView{
UITextRange *selectedRange = [textView markedTextRange];
//获取高亮部分
UITextPosition *pos = [textView positionFromPosition:selectedRange.start offset:0];
//如果在变化中是高亮部分在变,就不要计算字符
if (selectedRange && pos) {
return;
}
NSString *nsTextContent = textView.text;
NSInteger existTexNum = nsTextContent.length;
if (existTexNum > MAX_LIMIT_NUMS) {
//截取到最大位置的字符,
NSString *str = [nsTextContent substringToIndex:MAX_LIMIT_NUMS];
[textView setText:str];
}
//不显示负数
textNumLabel.text = [NSString stringWithFormat:@"%ld/%d",MAX(0, MAX_LIMIT_NUMS - existTexNum),MAX_LIMIT_NUMS];
}
网友评论