Get X and Y coordinates of a word in UITextView

前端 未结 2 1021
醉酒成梦
醉酒成梦 2020-12-01 09:47

I asked a developer (TwoLivesLeft, the creators of Codea) how they did syntax highlighting in their app. He replied :

@TD2 the Codea editor is impleme

2条回答
  •  北荒
    北荒 (楼主)
    2020-12-01 10:13

    @bddicken's https://stackoverflow.com/a/11487125/3549781 works like a charm. But the problem is, it doesn't work on iOS 7+ if the text contains a newline "\n". After a lot of searching I found a solution for that.

    You have to ensure the layout of textView before calling firstRectForRange: by

    [textView.layoutManager ensureLayoutForTextContainer:textView.textContainer];
    

    Courtesy : UITextView firstRectForRange not working when there's new line characters in the mix

    P.S : At first I added this as a comment to @bddicken's answer. As most people don't read comments I added this as an answer.

提交回复
热议问题