您的位置:首页 > 移动开发 > IOS开发

关于iOS对textView字数限制上的一些坑以及解决方法

2016-05-14 09:50 766 查看
现在的服务器一般是不会对字数做限制了,但是遇到一个需求需要对字数做限制,并且同时更新剩余的字数,刚开始我以为这个是非常简单的,然而这是个巨坑



这里一开始是使用代理方法监听字数的,然而这个方法当达到最大值200以后,虽然在英文状态下无法输入,但是在拼音联想状态下却依然会计算字数,并且由于拼音和拼音间存在空格,所以在高亮状态下也无法正常计算。

所以这里就采用了通知,这里计算还是比较准的,而且会出现一个-1的字数bug显示,但是真实的情况下是正常的,所以可以在字数达到限制后对提示字符进行写死限制,代码如下:

-(void)textChange:(NSNotification *)note{
    
    static
const NSInteger Max_Num_TextView =
200;
    
    self.isContentTextViewEnable =
true;
    //获取当前键盘类型
    UITextInputMode *mode = (UITextInputMode *)[UITextInputMode
activeInputModes][0];
    
    //获取当前键盘语言
    NSString *lang = mode.primaryLanguage;
    
    //如果语言是汉语(拼音)
    if ([lang
isEqualToString:@"zh-Hans"])
    {
        
        //取到高亮部分范围
        UITextRange *selectedRange = [self.contentTextView
markedTextRange];
        
        //取到高亮部分
        UITextPosition *position = [self.contentTextView
positionFromPosition:selectedRange.start
offset:0];
        
        //如果取不到高亮部分,代表没有拼音
        if (!position){
            
            //当期超过最大限制时
            if (self.contentTextView.text.length
> Max_Num_TextView) {
                
                //对超出部分进行裁剪
                self.contentTextView.text = [self.contentTextView.text
substringToIndex:Max_Num_TextView];
                
                //同时对可继续书写属性设为否,shouldChangeTextInRange方法会调用
                self.isContentTextViewEnable =
NO;
                
                //同时将下方提示label设置为0
                self.tipLabel.attributedText = [self
handleColorStr:[NSString
stringWithFormat:@"您还可以输入0个字"]];
            }
            
            //如果没超出,那么就计算剩余字数
            self.tipLabel.attributedText = [self
handleColorStr:[NSString
stringWithFormat:@"您还可以输入%ld个字",200-self.contentTextView.text.length]];

        }else{
            //表示还有高亮部分,暂不处理
        }
        
    }else{
        //如果语言不是汉语,直接计算
        if (self.contentTextView.text.length
> Max_Num_TextView) {
            
            self.contentTextView.text = [self.contentTextView.text
substringToIndex:Max_Num_TextView];
            self.isContentTextViewEnable =
NO;
            
             self.tipLabel.attributedText = [self
handleColorStr:[NSString
stringWithFormat:@"您还可以输入0个字"]];

        }
        
        self.tipLabel.attributedText = [self
handleColorStr:[NSString
stringWithFormat:@"您还可以输入%ld个字",200-self.contentTextView.text.length]];

    }
    
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString
*)text{

    

    
    if ([text
isEqualToString:@""]) {
        
        return
YES;
        
    }else{
        
        return
self.isContentTextViewEnable;
        
    }
}
//对字的位置颜色进行处理
-(NSMutableAttributedString*)handleColorStr:(NSString*)str{
    
    NSMutableAttributedString * attStr = [[NSMutableAttributedString
alloc]initWithString:str];
    
    if (attStr.length ==
11) {
        
        [attStr addAttribute:NSForegroundColorAttributeName
value:[UIColor
greenColor] range:NSMakeRange(6,
3)];
    }else
if(attStr.length ==
10){
        
        [attStr addAttribute:NSForegroundColorAttributeName
value:[UIColor
greenColor] range:NSMakeRange(6,
2)];
    }else{
        [attStr addAttribute:NSForegroundColorAttributeName
value:[UIColor
greenColor] range:NSMakeRange(6,
1)];
    }
    
    return attStr;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: