0

Is there an easy way ie convenience method to get the line number in a textview from the position perhaps using UITokenizer?

Something like:

UITextView *textView =  (UITextView *)recognizer.view;
CGPoint location = [recognizer locationInView:textView];
CGPoint position = CGPointMake(location.x, location.y);
UITextPosition *tapPosition = [textView closestPositionToPoint:position];
UITextRange *textRange = [textView.tokenizer rangeEnclosingPosition:tapPosition withGranularity:UITextGranularityLine inDirection:UITextLayoutDirectionRight];

 (pseudocode)
int lineNumber = [textView lineNumberOf:textRange];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
user6631314
  • 1,751
  • 1
  • 13
  • 44
  • Possible duplicate of: https://stackoverflow.com/questions/5837348/counting-the-number-of-lines-in-a-uitextview-lines-wrapped-by-frame-size – ridvankucuk Jan 06 '18 at 19:11
  • This should get you there: https://stackoverflow.com/questions/22807959/get-line-information-from-uitextview-and-nslayoutmanager – rmaddy Jan 06 '18 at 19:17
  • @ridvankucuk That's not at all what is being asked here. – rmaddy Jan 06 '18 at 19:17
  • I don't usually posts links to frameworks in answers, but in this case, it is rather complex to do for a UITextView. Check out this repo which can handle many different edge-cases: https://github.com/alldritt/TextKit_LineNumbers – Alec O Jun 22 '20 at 15:16

0 Answers0