如何找出UITextField中当前的插入位置? [英] How to figure out the current caret position in a UITextField?

查看:71
本文介绍了如何找出UITextField中当前的插入位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以编程方式将字符串粘贴到当前插入位置的文本字段或文本视图中。

I want to programmatically paste a string into a text field or text view at the current caret position.

有一种简单的方法吗?我需要知道当前的插入位置,但没有方法可以检索它,对吧?不想调用私有API。有没有合法的方法呢?

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?

推荐答案

使用 UITextView ' s selectedRange 方法。如果范围具有非零长度(在用户选择了某些文本的情况下),只需获取位置(或 NSMaxRange(),或替换整个范围...)

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

这篇关于如何找出UITextField中当前的插入位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆