美文网首页
UITextField、UITextView限制输入长度

UITextField、UITextView限制输入长度

作者: 程序渣渣猿 | 来源:发表于2018-12-03 22:36 被阅读14次
    
    #define kRecordingTitleLength 16
    
    - (void)actionForTitleTextFieldChaged:(UITextField *)textField {
        NSString *toBeString = textField.text;
        NSString * lang       = [[[UIApplication sharedApplication] textInputMode] primaryLanguage];
        if ([lang isEqualToString:@"zh-Hans"]) { // 如果输入的是简体中文
            UITextRange * selectRange = [textField markedTextRange];
            // 获取高亮部分
            UITextPosition * position = [textField positionFromPosition:selectRange.start offset:0];
            // 没有高亮选择的字,则对已输入的文字进行字数统计和限制
            if (!position) {
                if (toBeString.length > kRecordingTitleLength) {
                    self.titleTextField.text = [toBeString substringToIndex:kRecordingTitleLength];
                } else {
                    [self feedbackStringWithMessage:toBeString];
                }
            }
        } else { // 中文输入法以外的直接对其进行统计限制即可,不考虑其他情况
            if (toBeString.length > kRecordingTitleLength) {
                // 提醒最长字符
                self.titleTextField.text = [toBeString substringToIndex:kRecordingTitleLength];
            }else {
                [self feedbackStringWithMessage:toBeString];
                
            }
        }
    }
    
    
    - (void)feedbackStringWithMessage:(NSString *)message {
        
       // Do something
        
    }
    

    相关文章

      网友评论

          本文标题:UITextField、UITextView限制输入长度

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