UITextView输入文字长度限制

来源:互联网 发布:手机淘宝怎么投诉买家 编辑:程序博客网 时间:2024/05/19 18:11

参考:  http://blog.csdn.net/skyharute/article/details/51404357#comments

    

   [[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(DLGTextChange)name:UITextViewTextDidChangeNotificationobject:nil];

    self.myLabel=[[UILabelalloc]init];

    self.myLabel.font=[UIFontsystemFontOfSize:12];

    self.myLabel.textColor=[UIColorblackColor];

    [self.viewaddSubview:self.myLabel];

    self.myLabel.attributedText=[selfhandleColorStr:@"您还可以输入140个字"];

    [self.myLabelmas_makeConstraints:^(MASConstraintMaker *make) {

        make.right.equalTo(textview).offset(-6);

        make.bottom.equalTo(textview).offset(-6);

    }];


-(void)DLGTextChange{

    staticconst NSInteger Max_Num_TextView = 140;

    self.isContentTextViewEnable =true;

    //获取当前键盘类型

    UITextInputMode *mode = (UITextInputMode *)[UITextInputModeactiveInputModes][0];

    //获取当前键盘语言

    NSString *lang = mode.primaryLanguage;

    //如果语言是汉语(拼音)

    if ([langisEqualToString:@"zh-Hans"])

    {

        //取到高亮部分范围

        UITextRange *selectedRange = [textviewmarkedTextRange];

        //取到高亮部分

        UITextPosition *position = [textviewpositionFromPosition:selectedRange.startoffset:0];

        //如果取不到高亮部分,代表没有拼音

        if (!position){

            //当期超过最大限制时

            if (textview.text.length > Max_Num_TextView) {

                //对超出部分进行裁剪

                textview.text = [textview.textsubstringToIndex:Max_Num_TextView];

                //同时对可继续书写属性设为否,shouldChangeTextInRange方法会调用

                self.isContentTextViewEnable =NO;

                UIWindow *window = [[[UIApplicationsharedApplication] windows] lastObject];

                MBProgressHUD *hud = [MBProgressHUDshowHUDAddedTo:window animated:YES];

                hud.mode =MBProgressHUDModeText;

                hud.labelText =@"不能超过140个字";

                hud.margin = 10.f;

                hud.yOffset = 150.f;

                hud.removeFromSuperViewOnHide =YES;

                [hud hide:YESafterDelay:2];

                //同时将下方提示label设置为0

                self.myLabel.attributedText = [self handleColorStr:[NSStringstringWithFormat:@"您还可以输入0个字"]];

            }

            //如果没超出,那么就计算剩余字数

            self.myLabel.attributedText = [selfhandleColorStr:[NSStringstringWithFormat:@"您还可以输入%d个字",(int)(Max_Num_TextView - textview.text.length)]];   

        }else{

            //表示还有高亮部分,暂不处理

        }        

    }else{

        //如果语言不是汉语,直接计算

        if (textview.text.length > Max_Num_TextView) {

            textview.text = [textview.textsubstringToIndex:Max_Num_TextView];

            self.isContentTextViewEnable =NO;

            self.myLabel.attributedText = [selfhandleColorStr:[NSStringstringWithFormat:@"您还可以输入0个字"]];   

        }

        self.myLabel.attributedText = [selfhandleColorStr:[NSStringstringWithFormat:@"您还可以输入%d个字",(int)(Max_Num_TextView-textview.text.length)]];  

    }   

}

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

    if ([textisEqualToString:@""]) {

        returnYES;

    }else{

        returnself.isContentTextViewEnable;   

    }

}

//对字的位置颜色进行处理

-(NSMutableAttributedString*)handleColorStr:(NSString*)str{

    NSMutableAttributedString * attStr = [[NSMutableAttributedStringalloc]initWithString:str];

    if (attStr.length == 11) {

        [attStr addAttribute:NSForegroundColorAttributeNamevalue:[UIColorgreenColor] range:NSMakeRange(6, 3)];

    }elseif(attStr.length == 10){

        [attStr addAttribute:NSForegroundColorAttributeNamevalue:[UIColorgreenColor] range:NSMakeRange(6, 2)];

    }else{

        [attStr addAttribute:NSForegroundColorAttributeNamevalue:[UIColorgreenColor] range:NSMakeRange(6, 1)];

    }

    return attStr;

}


0 0
原创粉丝点击