iOS 輸入框限制輸入字節數

iOS中限制輸入框文字長度的方法有好多,百度一下,最經常使用的是這種:spa

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if ([textField isEqual:self.nameTextField]) {
        NSString *whole = [NSString stringWithFormat:@"%@%@",textField.text, string];
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        if ([whole dataUsingEncoding:NSUTF8StringEncoding].length > 14) {
            [textField resignFirstResponder];
            MAKE_TOAST_TOP(LOCALIZESTRING(@"NameLimit"));
            return NO;
        }
    }
    return YES;
}

可是這種方法有bug,當輸入最後一個漢字的時候,只能輸入拼音不能變成漢字。code

通過仔細的搜索以後會發現另外一中方法:orm

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:) name:UITextFieldTextDidChangeNotification object:self.nameTextField];
- (void)textFieldDidChange:(NSNotification *)noti {
    NSString *language = [[UITextInputMode currentInputMode] primaryLanguage];
    NSString *string = self.nameTextField.text;
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    if ([language rangeOfString:@"zh-Hans"].length != 0 || [language rangeOfString:@"zh-Hant"].length != 0) {
        UITextRange *selectedRange = self.nameTextField.markedTextRange;
        UITextPosition *position = [self.nameTextField positionFromPosition:selectedRange.start offset:0];
        if (!position) {
            if (data.length > 14) {
                self.nameTextField.text = [string substringToIndex:14];
                [self.nameTextField resignFirstResponder];
                MAKE_TOAST_TOP(LOCALIZESTRING(@"NameLimit"));
            }
        } else {

        }
    } else {
        if ([self.nameTextField.text dataUsingEncoding:NSUTF8StringEncoding].length > 14) {
            [self.nameTextField resignFirstResponder];
            MAKE_TOAST_TOP(LOCALIZESTRING(@"NameLimit"));
        }
    }
}

這種方法沒有前面說的那種bug,會截取指定長度字符串,可是若是是中英文混合輸入的話就沒辦法根據字節數計算對應的字符串長度,咱們但願超過指定字節數時,截取到最後一次輸入以前的長度。server

能夠將上面兩種方法結合起來來實現咱們的需求:blog

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (range.length != 0) { self.lastInputStringRange = range; }
    return YES;
}
- (void)textFieldDidChange:(NSNotification *)noti {
    NSString *language = [[UITextInputMode currentInputMode] primaryLanguage];
    NSString *string = self.nameTextField.text;
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    if ([language rangeOfString:@"zh-Hans"].length != 0 || [language rangeOfString:@"zh-Hant"].length != 0) {
        UITextRange *selectedRange = self.nameTextField.markedTextRange;
        UITextPosition *position = [self.nameTextField positionFromPosition:selectedRange.start offset:0];
        if (!position) {
            if (data.length > 14) {
                self.nameTextField.text = [string substringToIndex:self.lastInputStringRange.location];
                [self.nameTextField resignFirstResponder];
                MAKE_TOAST_TOP(LOCALIZESTRING(@"NameLimit"));
            }
        } else {

        }
    } else {
        if ([self.nameTextField.text dataUsingEncoding:NSUTF8StringEncoding].length > 14) {
            self.nameTextField.text = [string substringToIndex:14];
            [self.nameTextField resignFirstResponder];
            MAKE_TOAST_TOP(LOCALIZESTRING(@"NameLimit"));
        }
    }
}

 

使用變量lastInputStringRange記錄最近一次輸入的範圍,超過指定範圍截取字符串時,截取到該位置便可。字符串

相關文章
相關標籤/搜索