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

本文介绍了在iOS开发中遇到的textView字数限制问题,详细解析了使用代理方法和通知方式存在的问题及解决方案。通过监听textView内容变化,结合键盘语言判断,确保在不同输入状态下准确限制字数并实时更新剩余字数提示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


这里一开始是使用代理方法监听字数的,然而这个方法当达到最大值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;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值