1

I'm trying to get an actual point of selected character in NSTextContainer:

func handleTouch(gestureRecognizer: UIGestureRecognizer) {
   var location = gestureRecognizer.locationInView(self)
   let startPoint = self.layoutManager.locationForGlyphAtIndex(0)
   location = CGPoint(x: location.x - startPoint.x, y: location.y - startPoint.y)
   var fraction: CGFloat = 0
   let index = self.layoutManager.glyphIndexForPoint(location, inTextContainer: textContainer, fractionOfDistanceThroughGlyph: &fraction)
}

And it works works as it should, UNTIL first linebreak set by

NSParagraphStyle.hyphenationFactor = 2

Every hyphen moves index for one point and glyphIndexForPoint get wrong index. How to get right index with linebreak-mode enabled?

Jauzee
  • 120
  • 2
  • 18

1 Answers1

0

Try the similar function characterIndexForPoint(_:inTextContainer:fractionOfDistanceBetweenInsertionPoints:)

For more details, you can see this question

Community
  • 1
  • 1
Tikhonov Aleksandr
  • 13,945
  • 6
  • 39
  • 53