gpt4 book ai didi

iphone - UITextField 光标向后移动

转载 作者:行者123 更新时间:2023-11-29 13:22:27 25 4
gpt4 key购买 nike

我想防止 UITextField 中的光标向后移动。或者我需要防止在已经输入的两个字符之间输入文本。我该怎么做?

最佳答案

没有任何属性或方法可以阻止用户在特定文本字段上使用放大镜。您可以尝试将字符串中的所有字符都屏蔽掉:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if(range.location < textField.text.length) return NO; else return YES;
}

经过一些研究,我发现了这个问题:Disable Magnifying Glass in UITextField .已接受答案的作者说有一种方法可以做到:

You can also set userInteractionEnabled to NO so that the user can't tap the field. Call becomeFirstResponder manually so that the field gets focus since the user can't tap to focus.

但是,正如我发现的那样,它不起作用,因为 setUserInteractionEnabled:NO 阻止文本字段成为第一响应者。第二次研究后我发现了另一个问题:Show UITextField keyboard on firstResponder even when userInteractionEnabled = NO . MaxGabriel 用户声称他找到了这样做的方法:

What I do is add that hidden text field to the view, and call becomeFirstResponder on it. The user has no idea this text field exists. In the delegate callback from the text field, I take the text the user typed in and add it to a UITextView (though you could add the text to whatever you wanted, like a UITextField like in your question). I turn off userInteractionEnabled for the visible text view. This creates the effect you desire.

Stack Overflow 是一个庞大的数据库,在提问之前进行一些研究可能会对您的 future 有所帮助。

关于iphone - UITextField 光标向后移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14069949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com