首页 > 解决方案 > 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.

screen1

screen2

标签: iosobjective-cswift

解决方案


决定:

第 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。


推荐阅读