如何在uitextview中的任意光标位置插入文本?
我想实现代码,通过它我可以开始在 iphone sdk 中的 UITextView 中光标的任何位置插入文本,
有什么想法吗? 提前谢谢您..
我引用了此链接:iPhone SDK:如何创建一个在点击位置插入文本的 UITextView?
但没有得到它。
i want to implement Code by which i can start to insert text at any position of cursor in UITextView in iphone sdk
any idea?
thank you in advance..
i refereed this link: iPhone SDK: How to create a UITextView that inserts text where you tap?
But not Getting it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Dan 的答案是手动更改文本。它与
UITextView
的UndoManager
配合得不好。实际上,使用
UITextInput
协议API插入文本非常容易,UITextView
和UITextField
都支持该API。注意:它是
UITextInput
协议中的selectedTextRange
,而不是selectedRange
Dan's answer is manually changing the text. It's not playing well with
UITextView
'sUndoManager
.Actually it's very easy to insert text with
UITextInput
protocol API, which is supported byUITextView
andUITextField
.Note: It's
selectedTextRange
inUITextInput
protocol, rather thanselectedRange
这是我与自定义键盘一起使用的,似乎工作正常,可能有更干净的方法,不确定。
This is what I use with a custom keyboard, seems to work ok, there may be a cleaner approach, not sure.
这是 Glofindel 在 Swift3 中的答案。此处插入的文本将从剪贴板中拉出。
Here is the Glorfindel's answer in Swift3. The text its inserting here it pulls out of the clipboard.
最简单的方法(但不会替换选定的文本)是使用
insertText:
方法:UITextView
符合UITextInput
本身符合UIKeyInput
。The simplest way (but it won't replace selected text) is to use the
insertText:
method:UITextView
conforms toUITextInput
which itself conforms toUIKeyInput
.