简体   繁体   English

如何弄清楚UITextField中当前插入符号的位置?

[英]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. 不想调用私有API。 Is there any legal way to do it? 有什么合法的方法吗?

Use UITextView 's selectedRange method. 使用UITextViewselectedRange方法。 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...) 如果范围的长度不为零(在用户选择了一些文本的情况下),只需获取位置(或NSMaxRange() ,或替换整个范围...)。

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. 从文本字段或文本视图中获取字符串,然后通过提供索引插入要粘贴的字符串,然后清除文本字段,然后传递插入的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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