Now I already detect long tap in UITextView
- (void)viewDidLoad
{
[super viewDidLoad];
UILongPressGestureRecognizer *LongPressgest
SWIFT 4
A copy of @cayeric's answer written in swift for your convenience.
func getWord(at position: CGPoint, in textView: UITextView) -> String?{
var point = position
//eliminate scroll offset
point.y += textView.contentOffset.y
//get location in text from textposition at point
guard let tapPos = textView.closestPosition(to: point) else {
return nil
}
//fetch the word at this position (or nil, if not available)
guard let wordRange = textView.tokenizer.rangeEnclosingPosition(tapPos, with: .word, inDirection: UITextWritingDirection.rightToLeft.rawValue) else {
return nil
}
return textView.text(in: wordRange)
}