ios - How to disable hint of correct text in UITextView?
问题描述
I am using a UITextView to display text in a table cell.
By clicking on the cell (by textView) I open the screen for editing text by overriding the delegate method (UITextViewDelegate) textViewDidBeginEditing (setting my handler there).
If the word was entered incorrectly, then by clicking on this word a hint pops up with the correct word. I suppose this is a UIMenuController (99%), but this menu is not visible in the hierarchy. Not only does the tooltip pop up, but also with a delay, which leads to the fact that it is already on top of the open new screen for editing text.
func textViewDidBeginEditing(_ textView: UITextView) {
textView.resignFirstResponder()
selectHandler?()
}
1) textView.isSelectable - does not suit me, since then textViewDidBeginEditing does not work.
2) textView.isEditable - also does not fit.
3) with canPerformAction - nothing happens (I tried a bunch of different ways). Assume the code below.
override var canBecomeFirstResponder: Bool {
return false
}
Or suppose this code doesn’t work either.
override public func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(copy(_:)) || action == #selector(paste(_:)) || action == #selector(UIResponderStandardEditActions.paste(_:)) ||
action == #selector(replace(_:withText:)) ||
action == #selector(UIResponderStandardEditActions.cut(_:)) ||
action == #selector(UIResponderStandardEditActions.select(_:)) ||
action == #selector(UIResponderStandardEditActions.selectAll(_:)) ||
action == #selector(UIResponderStandardEditActions.delete(_:)) ||
action == #selector(UIResponderStandardEditActions.makeTextWritingDirectionLeftToRight(_:)) ||
action == #selector(UIResponderStandardEditActions.makeTextWritingDirectionRightToLeft(_:)) ||
action == #selector(UIResponderStandardEditActions.toggleBoldface(_:)) ||
action == #selector(UIResponderStandardEditActions.toggleItalics(_:)) ||
action == #selector(UIResponderStandardEditActions.toggleUnderline(_:)) ||
action == #selector(UIResponderStandardEditActions.increaseSize(_:)) ||
action == #selector(UIResponderStandardEditActions.decreaseSize(_:)) {
return false
}
return true }
4) textView.autocorrectionType = .no - not for this, not because of my problem.
5) This method also does not work, see the code below.
func textViewDidChangeSelection(_ textView: UITextView) {
view.endEditing(true)
}
6) UIMenuController.shared.isMenuVisible = false - this did not work in different ways.
7) textView.isUserInteractionEnabled = false - it definitely does not suit me.
I could remove the selection from incorrect text with a red marker (in addition to this tooltip), which also had to not be shown, using this code below.
func textViewDidChangeSelection(_ textView: UITextView) {
let selectedRange: UITextRange? = textView.selectedTextRange
var selectedText: String? = nil
if let selectedRange = selectedRange {
selectedText = textView.text(in: selectedRange)
}
if (selectedText?.count ?? 0) > 1 && (selectedText?.count ?? 0) <= textView.text.count {
textView.selectedRange = NSRange(location: 0, length: 0)
}
}
Question: Any ideas? tell them please.
解决方案
决定:
第 1 步(为 UITextView 创建一个自定义类):
import Foundation
import UIKit
final class CustomTextView: UITextView {
var selectHandler: (() -> Void)?
override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
if let _ = gestureRecognizer as? UIPanGestureRecognizer {
return true
}
selectHandler?()
return false
}
}
第 2 步(将您的操作分配给单元格本身中的 selectHandler):
textView.selectHandler = selectHandler(Your method you want to call)
不要忘记为 textView 插座 (CustomTextView) 安装一个新类。
包括xib。
推荐阅读
- bitbucket-pipelines - 如何在我的 bitbucket-pipelines.yml 文件中使用 build.gradle 文件中的版本
- android - Android Recyclerview 很慢
- android - Android 模拟器黑屏(模拟引擎失败) - Riot Vanguard
- javascript - 如何使用 async 和 await 正确地从 fetch api 获取 console.log 响应?
- javascript - javaScript:对象的类和使用函数之间的区别?
- html - 在响应模式下图像不完整
- android - 拍摄一些照片后,Android camerax 停止工作
- html - Flex 页眉/内容/页脚在 IE 中导致不需要的滚动条,但在其他浏览器中不会
- python - Python,ValueError:没有足够的值来解包(预期 2,得到 1)
- java - 如何让 Miglayout 单元格垂直向下跨越所有行