iOS Components and Frameworks: Understanding the Advanced Features of the iOS SDK
by Kyle Richter, Joe Keeley
Detecting Hits
Hit detection has always been complex to implement and often required for elaborate text-driven apps. iOS 7 added support for per-character hit detection. In order to support this functionality a subclassed UITextView is created, called ICFCustomTextView in the sample project. The UITextView implements a touchesBegan event method.
When a touch begins, the location in the view is captured and it is adjusted down the y axis by ten to line up with the text elements. A method is invoked on the layoutManager that is a property of the text view, characterIndexForPoint: inTextContainer: fractionOfDistanceBetweenInsertionPoints:. This returns the index of the character that was selected.
After the character index has been determined, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access