如何禁用UITextField的自动填充、语音朗读、拼写检查及光标操作?
解决方案
1. 禁用系统文本辅助功能
在初始化阶段设置核心属性,从根源关闭自动填充、拼写检查、语音输入相关功能:
autocorrectionType = .no:关闭自动纠错spellCheckingType = .no:关闭拼写检查autocapitalizationType = .none:关闭自动大写textContentType = .rawValue(""):禁用所有自动填充类型smartQuotesType = .no/smartDashesType = .no:关闭智能符号替换- 清空键盘辅助按钮组:移除语音输入等快捷入口
2. 拦截所有文本操作菜单
完善canPerformAction方法,明确禁止复制、粘贴、朗读、选择等所有文本操作:
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool { guard actionEnabled else { let forbiddenActions: [Selector] = [ #selector(UIResponderStandardEditActions.copy(_:)), #selector(UIResponderStandardEditActions.paste(_:)), #selector(UIResponderStandardEditActions.cut(_:)), #selector(UIResponderStandardEditActions.select(_:)), #selector(UIResponderStandardEditActions.selectAll(_:)), #selector(UITextField.speak(_:)), #selector(UITextField.stopSpeaking(_:)) ] return !forbiddenActions.contains(action) && super.canPerformAction(action, withSender: sender) } return super.canPerformAction(action, withSender: sender) }
3. 强制顺序输入/删除逻辑
重写shouldChangeCharacters(in:)方法,仅允许两种合法修改:
- 新增字符必须追加到文本末尾
- 删除字符只能移除最后一位
override func shouldChangeCharacters(in range: NSRange, replacementString string: String) -> Bool { guard !actionEnabled else { return super.shouldChangeCharacters(in: range, replacementString: string) } let currentText = self.text ?? "" // 输入字符:只能从末尾追加 if !string.isEmpty { return range.location == currentText.count } // 删除字符:只能删除最后一位 else { return range.location == currentText.count - 1 && range.length == 1 } }
4. 彻底禁用光标与文本选择
保留原有光标隐藏逻辑,额外拦截选择文本的手势:
override func selectionRects(for range: UITextRange) -> [UITextSelectionRect] { return [] } override func caretRect(for position: UITextPosition) -> CGRect { return .zero } override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool { guard actionEnabled else { // 禁止长按、点击选择文本的手势 if gestureRecognizer is UILongPressGestureRecognizer || gestureRecognizer is UITapGestureRecognizer { return false } return super.gestureRecognizerShouldBegin(gestureRecognizer) } return super.gestureRecognizerShouldBegin(gestureRecognizer) }
完整自定义UITextField代码
class GradientTextField: UITextField { var actionEnabled: Bool = true override init(frame: CGRect) { super.init(frame: frame) setupBaseProperties() } required init?(coder: NSCoder) { super.init(coder: coder) setupBaseProperties() } private func setupBaseProperties() { // 禁用系统文本辅助功能 autocorrectionType = .no spellCheckingType = .no autocapitalizationType = .none textContentType = .rawValue("") smartQuotesType = .no smartDashesType = .no // 移除键盘辅助按钮 inputAssistantItem.leadingBarButtonGroups = [] inputAssistantItem.trailingBarButtonGroups = [] } override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool { guard actionEnabled else { let forbiddenActions: [Selector] = [ #selector(UIResponderStandardEditActions.copy(_:)), #selector(UIResponderStandardEditActions.paste(_:)), #selector(UIResponderStandardEditActions.cut(_:)), #selector(UIResponderStandardEditActions.select(_:)), #selector(UIResponderStandardEditActions.selectAll(_:)), #selector(UITextField.speak(_:)), #selector(UITextField.stopSpeaking(_:)) ] return !forbiddenActions.contains(action) && super.canPerformAction(action, withSender: sender) } return super.canPerformAction(action, withSender: sender) } override func shouldChangeCharacters(in range: NSRange, replacementString string: String) -> Bool { guard !actionEnabled else { return super.shouldChangeCharacters(in: range, replacementString: string) } let currentText = self.text ?? "" if !string.isEmpty { return range.location == currentText.count } else { return range.location == currentText.count - 1 && range.length == 1 } } override func selectionRects(for range: UITextRange) -> [UITextSelectionRect] { return [] } override func caretRect(for position: UITextPosition) -> CGRect { return .zero } override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool { guard actionEnabled else { if gestureRecognizer is UILongPressGestureRecognizer || gestureRecognizer is UITapGestureRecognizer { return false } return super.gestureRecognizerShouldBegin(gestureRecognizer) } return super.gestureRecognizerShouldBegin(gestureRecognizer) } }
使用说明
需要启用限制时,设置textField.actionEnabled = false;恢复正常功能时设为true即可。
内容的提问来源于stack exchange,提问作者Philipp
相关产品推荐
相关产品推荐

