zoukankan      html  css  js  c++  java
  • iOS 限制输入框不能输入中文

    开发中遇到这个问题,想着还是总结下,刚开始只是限制UITextField的键盘为

    UIKeyboardTypeASCIICapable,可是当用户切换了中文键盘后依然没解决问题,于是我给输入框加了监听事件,获取输入框最新的输入内容,检测输入的内容中是否含有中文,如果有中文就替换成空字符串,具体实现如下:

    infoView.userTF.keyboardType = UIKeyboardTypeASCIICapable;

     //监听输入内容

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFiledEditChanged:)

                                                    name:@"UITextFieldTextDidChangeNotification"

                                                  object:infoView.userTF];

    -(void)textFiledEditChanged:(NSNotification*)notification

    {

        UITextField*textField = notification.object;

        NSString*str = textField.text;

        for (int i = 0; i<str.length; i++)

        {

            NSString*string = [str substringFromIndex:i];

            NSString *regex = @"[u4e00-u9fa5]{0,}$"; // 中文

            // 2、拼接谓词

            NSPredicate *predicateRe1 = [NSPredicate predicateWithFormat:@"self matches %@", regex];

            // 3、匹配字符串

            BOOL resualt = [predicateRe1 evaluateWithObject:string];

            

            if (resualt)

            {

        //是中文替换为空字符串

                str =  [str stringByReplacingOccurrencesOfString:[str substringFromIndex:i] withString:@""];

            }

        }

        textField.text = str;

    }

  • 相关阅读:
    Shell脚本编程(三):shell参数传递
    Java代码里利用Fiddler抓包调试设置
    Shell脚本编程(二):shell变量
    Shell脚本编程(一):初识shell script
    JAVA使用SCANNER接收中文并输出时出现乱码
    RandomAccessFile类理解
    Vue(九):样式绑定v-bind示例
    Dockerfiles ENV和ARG的应用
    dockerfile中设置python虚拟环境+gunicorn启动
    Docker容器 暴露多个端口
  • 原文地址:https://www.cnblogs.com/cui-cui/p/7545692.html
Copyright © 2011-2022 走看看