简体   繁体   中英

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 current 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM