Ios 最大输入后禁用UITextField光标移动

Ios 最大输入后禁用UITextField光标移动,ios,swift,uitextfield,Ios,Swift,Uitextfield,我为固定大小的输入创建了一个自定义UITextField组件。TextField的宽度、字体大小、字距和字符数都是固定的。我可以阻止用户输入超过8个字符(请参见下面的ShouldChangeCharacters方法),但是在第8个字符之后,光标会自动移动到下一个位置,这会产生以下问题 我想显示所有输入的字符,但不减小其大小。所以 [12345678] 应该显示,而不是 [2 3 4 5 6 7 8 |] 我试着把光标放在第8个字符的右边,但是这会改变所有的紧排并影响所有其他字符 正确的处理方法

我为固定大小的输入创建了一个自定义UITextField组件。TextField的宽度、字体大小、字距和字符数都是固定的。我可以阻止用户输入超过8个字符(请参见下面的ShouldChangeCharacters方法),但是在第8个字符之后,光标会自动移动到下一个位置,这会产生以下问题

我想显示所有输入的字符,但不减小其大小。所以

[12345678] 应该显示,而不是 [2 3 4 5 6 7 8 |]

我试着把光标放在第8个字符的右边,但是这会改变所有的紧排并影响所有其他字符

正确的处理方法是什么?如何防止光标移动,使UITextField不会向右滚动

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    guard let textFieldText = textField.text,
        let rangeOfTextToReplace = Range(range, in: textFieldText),
        !string.containsSpecialCharacters else {
            return false
    }
    let substringToReplace = textFieldText[rangeOfTextToReplace]
    let count = textFieldText.count - substringToReplace.count + string.count

    return count <= 8
}
func textField(textField:UITextField,shouldChangeCharacters范围:NSRange,replacementString:string)->Bool{
guard let textFieldText=textField.text,
让rangeOfTextToReplace=范围(范围,in:textFieldText),
!string.containsSpecialCharacters其他{
返回错误
}
let substringToReplace=textFieldText[rangeOfTextToReplace]
let count=textFieldText.count-substringstorereplace.count+string.count

返回计数如果您尝试此代码会发生什么

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
{
    if range.location < 8 {
        return true
    }
    // return NO to not change text
    return false
}
func textField(textField:UITextField,shouldChangeCharacters范围:NSRange,replacementString:string)->Bool
{
如果范围位置<8{
返回真值
}
//返回NO以不更改文本
返回错误
}

请尝试以下代码:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if textField.text?.count ?? 0 <= 7{
            if textField.text?.count ?? 0 == 7{
                DispatchQueue.main.async {
                    textField.resignFirstResponder()
                }
            }
            return true
        }
        textField.resignFirstResponder()
        return false
    }

func textField(textField:UITextField,shouldChangeCharacters范围:NSRange,replacementString:string)->Bool{

如果textField.text?.count??0我通过更改最后一个字符后的字距来解决问题。因此,现在,光标只跳1pt,而不是跳40pt(或在下面方法中设置的任何值)


试试这个
guard let currentText=(textField.text作为NSString?).replacingCharacters(in:range,with:string)else{return true}让newLength=currentText.count+string.count-range.length返回newLength这将阻止输入第8个字符。我希望允许8个字符,那么光标不应移动到下一个空格。这只是隐藏键盘,这不是预期的行为。我只是试图阻止光标在最后一个字符后跳转。用户仍然可以尝试输入m删除或删除字符。
@objc private func textFieldDidChange() {

    guard let text = textField.text else { return }

    if text.count == Constant.deviceCodeCharacterCount {
        let attributedString = NSMutableAttributedString(string: text)
        attributedString.addAttribute(.kern, value: Constant.deviceCodeCharacterSpacing, range: NSRange(location: 0, length: text.count - 1))
        attributedString.addAttribute(.kern, value: 1, range: NSRange(location: Constant.deviceCodeCharacterCount - 1, length: 1))
        textField.attributedText = attributedString
    }
}