This solution is overwhelmed and overcomplicated. It works but system it is doing it and TextKit has all needed API.
Few classes allow you to map a text inside UILabel
and make the text inside tappable, means it adds the ability to detect which word is tapped. It’s both very simple and powerful.
Demo movie tstextmapper.mov
is attached in the repo and available on youtube.
- Create
TSTextMapper
. - Select one of the few methods how text mapper should work
mapTextAndMakeAllTappable
ormapTextWithTappableRanges
- Call
textForPoint
method when you want to get a word.
-
mapTextAndMakeAllTappable
method makes all text tappable. -
mapTextWithTappableRanges
method makes tappable text only in passed ranges. -
textForPoint
method returnsTSTextProxy
. It contains propertyvalue
.
If you noticed that something may be wrong with detecting touches (word is not selected or not this word is selected) you can check few things:
- In
TSTextMapper
isdebug
method and it's commented insidemap
method. Put a breakpoint on linedebugView.snapshot()
indebug
method. Method is called for the first time whenUILabel
is touched. - In
TSTextAnalizer
isdebug
method and it's commented insidemapLines
method. Uncomment it. It's called whenUILabel
is touched for the first time. Entire text should be printed out and the text should be presented in lines the same like it looks insideUILabel
. - In
TSTextRefDetector
there isdebug
method which is commented intextRefs
method. Uncomment it. All words and whitespaces should be printed out.