How to figure out the current caret position in a UITextField?
I want to programmatically paste a string into a text field or text view at the c开发者_如何学Gourrent caret position.
Is there an easy way to do this? I would need to know the current caret position, but there's no method to retrieve it, right? Don't want to call private API. Is there any legal way to do it?
Use UITextView
's selectedRange
method. If the range has a non-zero length (in the case of the user having selected some text), just take the location (or NSMaxRange()
, or replace the entire range...)
Instead of pasting option. Get the string from textfield or textview insert the string which you want to paste it by providing the index and then clear the textfield then pass the string which you inserted.
精彩评论