0

向我的子类添加点击识别器后UITextView,我试图获取被点击的字符:

var textRecognizer: UITapGestureRecognizer!
required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    textContainer.lineFragmentPadding = 0
    textContainerInset = .zero

    textRecognizer = UITapGestureRecognizer(target: self, action: #selector(textTapped))
    textRecognizer.numberOfTapsRequired = 1
    addGestureRecognizer(textRecognizer)
}

@objc func textTapped(recognizer: UITapGestureRecognizer) {
    let location = recognizer.location(in: self)
    if let cRange = characterRange(at: location) {
        let cPosition = offset(from: beginningOfDocument, to: cRange.start)
        let cChar = text[Range(NSRange(location: cPosition, length: 1), in: text)!]
        print(cChar)
    }
}

问题是,如果我的属性文本是"Hello world\nWelcome to Stack Overflow"并且我点击一个字母的左侧,比如 letter 的左侧f,然后characterRange(at: location)返回前一个字母r而不是返回f

4

1 回答 1

0

From my perspective, characterRange(at:) is buggy:

  • If you give it a point on the left half of character at index n, it returns range (n-1, n)
  • If you give it a point on the right half of character at index n, it returns range (n, n+1)
  • If you give it a point on the left half of character at index beginningOfDocument, it returns nil
  • If you give it a point on the right half of character at index endOfDocument, it returns (endOfDocument, endOfDocument+1)

The discrepancy of the behaviors at the extremities of the textInput demonstrate that there is a bug somewhere.

It behaves like a sort of "cursor position at point" function, which makes it unreliable to determine which character is actually at this point: is it the character before the cursor or the character after the cursor?

closestPosition(to:) suffers from the exact same issue.

A working alternative is layoutManager.characterIndex(for:in:fractionOfDistanceBetweenInsertionPoints:). Credit to vacawama:

@objc func textTapped(recognizer: UITapGestureRecognizer) {
    var location = recognizer.location(in: self)
    location.x -= textContainerInset.left
    location.y -= textContainerInset.top
    let cPosition = layoutManager.characterIndex(for: location, in: textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
    let cChar = text[Range(NSRange(location: cPosition, length: 1), in: text)!]
    print(cChar)
}
于 2018-03-29T11:08:18.173 回答