美文网首页
IOS TextField限定汉字个数

IOS TextField限定汉字个数

作者: 蜗牛1992 | 来源:发表于2016-09-03 22:07 被阅读43次

    textField.placeholder = @"请输入标签(最多输入3个汉字)";
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFieldEditChanged:) name:UITextFieldTextDidChangeNotification object:textField];

    -(void)textFieldEditChanged:(NSNotification *)obj{
    UITextField *textField = (UITextField *)obj.object;
    NSString *toBeString = textField.text;
    NSString *lang = [[UIApplication sharedApplication]textInputMode].primaryLanguage;
    if ([lang isEqualToString:@"zh-Hans"]) {
    UITextRange *selectedRange = [textField markedTextRange];
    UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
    if (!position) {
    if (toBeString.length>TagLength) {
    textField.text = [toBeString substringToIndex:TagLength];
    }
    }
    else{

        }
    }
    else{
        if (toBeString.length > TagLength) {
            textField.text = [toBeString substringToIndex:TagLength];
        }
    }
    

    }

    相关文章

      网友评论

          本文标题:IOS TextField限定汉字个数

          本文链接:https://www.haomeiwen.com/subject/eoqlettx.html