skip to Main Content

UITextView, Apple quirks and cutting one’s losses

I’m in the middle of a project which excites me a lot, especially because I want to have it finished in the next couple of days, so that with a bit of luck the app would go live to the appstore before December 22 (the dreaded date when iTunesConnect will shut down for an entire week). I’ve only had 3.5 hours of sleep last night, and about 5 the night before.

One of the things I was doing at 5AM in the morning was trying to find the current point location of the cursor inside a UITextView (iOS 4.3+ or even 5.0 – whichever works).

I couldn’t.

Now, it’s not unusual to not be able to do things in iOS. What is unusual is that Apple’s docs said I should have been able to.

Let me explain. See, UITextView, according to the documentation, conforms to UITextInput protocol. UITextInput protocol, according to the documentation, has a required property selectedTextRange that gives “the range of the selected text in a document” and, in particular, the caret(insertion-point). It should be straightforward.  (PS. do not mistake selectedTextRange for selectedRange which returns just the range of the selection inside the text string.

To my frustration and despair, it didn’t work. My code would crash again and again complaining that selectedTextRange is an unrecognized selector. Even more frustrating, Google doesn’t offer any clue (except for one stackoverflow question and the fact that people enjoy giving stupid answers although they don’t understand the question). Or that others don’t think it’s possible.

So I gave up implementing what I wanted, cut my losses and moved on to implementing some other features. But if you have any idea about a decent way to achieve this (and, no, I don’t really want to use a custom 3rd party uitextview clone just to be able to know the CGPoint position of the cursor), I’d greatly appreciate your input.

Until then, ain’t it weird that a required property in a required protocol doesn’t seem to be  fully implemented in Apple’s one of the most important core UI objects?

Back To Top