1

Using iOS 11, Swift 4, Xcode 9

Question upfront: Is there a way (or workaround) to force the layoutManager to obey the new orphaned text rule?

I am implementing a system for detecting which character a user taps within a label as in this answer: https://stackoverflow.com/a/42240972/7515904

I am printing the character index on each tap to compare to where I actually tapped. For cells 1-3 (image below), it prints exactly the correct index.

Cell 4 is affected by Apple's new (iOS11) orphaned text implementation:

  • The word "septum" should be on line 1, but it is dropped to line 2 so that the word "infarction" isn't alone.
  • The printed character index acts like "septum" is on line 1 (i.e. tapping white space to the right of "or" gives a higher index, and tapping text in the area of "infarction" does not.)

The functional line of code to detect character index is:

let indexOfCharacter = layoutManager.characterIndex(for: locationOfTouchInTextContainer, in: textContainer,  fractionOfDistanceBetweenInsertionPoints: nil)

My assumption is that somehow the layoutManager doesn't use this orphaned text method, despite the fact that I made the attributedText within this layoutManager the same as the actual cell label.

Question: Is there a way (or workaround) to force the layoutManager to obey this new orphaned text rule? Thanks for any guidance!

NOTE: Ignore the blue underlined text - I am trying to get the function working before the visual representation. enter image description here

Coltuxumab
  • 615
  • 5
  • 16
  • I think you should use UITextView with instead of UILabel to detect which character tapped by user. – Sagar Chauhan Apr 18 '18 at 14:10
  • Thanks @paul. I have been considering that, and it may be the only solution. The problem is that it seems to cause performance issues, which is a problem since I have lots of cells with attributed text. – Coltuxumab Apr 18 '18 at 14:13
  • @PaulMarshal although your suggestion was not the true solution to my question, I did end up using a UITextView instead with a .link attribute and custom URL, and it seems to be working well. If you want to suggest the UITextView as an answer, I'll mark it as correct until someone else comes up with the true answer. Thanks! – Coltuxumab Apr 18 '18 at 23:40

0 Answers0