如何在uitextview中的任意光标位置插入文本?

发布于 2024-12-26 07:32:10 字数 287 浏览 5 评论 0原文

我想实现代码,通过它我可以开始在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

゛时过境迁 2025-01-02 07:32:10

Dan 的答案是手动更改文本。它与 UITextViewUndoManager 配合得不好。

实际上,使用UITextInput协议API插入文本非常容易,UITextViewUITextField都支持该API。

[textView replaceRange:textView.selectedTextRange withText:insertingString];

注意:它是 UITextInput 协议中的 selectedTextRange,而不是 selectedRange

Dan's answer is manually changing the text. It's not playing well with UITextView's UndoManager.

Actually it's very easy to insert text with UITextInput protocol API, which is supported by UITextView and UITextField.

[textView replaceRange:textView.selectedTextRange withText:insertingString];

Note: It's selectedTextRange in UITextInput protocol, rather than selectedRange

梦里寻她 2025-01-02 07:32:10

这是我与自定义键盘一起使用的,似乎工作正常,可能有更干净的方法,不确定。

NSRange range = myTextView.selectedRange;  
NSString * firstHalfString = [myTextView.text substringToIndex:range.location];  
NSString * secondHalfString = [myTextView.text substringFromIndex: range.location];  
myTextView.scrollEnabled = NO;  // turn off scrolling  

NSString * insertingString = [NSString stringWithFormat:@"your string value here"];

myTextView.text = [NSString stringWithFormat: @"%@%@%@",  
                 firstHalfString,  
                 insertingString,  
                 secondHalfString];  
range.location += [insertingString length];  
myTextView.selectedRange = range;  
myTextView.scrollEnabled = YES;  // turn scrolling back on.

This is what I use with a custom keyboard, seems to work ok, there may be a cleaner approach, not sure.

NSRange range = myTextView.selectedRange;  
NSString * firstHalfString = [myTextView.text substringToIndex:range.location];  
NSString * secondHalfString = [myTextView.text substringFromIndex: range.location];  
myTextView.scrollEnabled = NO;  // turn off scrolling  

NSString * insertingString = [NSString stringWithFormat:@"your string value here"];

myTextView.text = [NSString stringWithFormat: @"%@%@%@",  
                 firstHalfString,  
                 insertingString,  
                 secondHalfString];  
range.location += [insertingString length];  
myTextView.selectedRange = range;  
myTextView.scrollEnabled = YES;  // turn scrolling back on.
我爱人 2025-01-02 07:32:10

这是 Glofindel 在 Swift3 中的答案。此处插入的文本将从剪贴板中拉出。

if let textRange = myTextView.selectedTextRange {
   myTextView.replace(textRange, withText:UIPasteboard.general.string!)
}

Here is the Glorfindel's answer in Swift3. The text its inserting here it pulls out of the clipboard.

if let textRange = myTextView.selectedTextRange {
   myTextView.replace(textRange, withText:UIPasteboard.general.string!)
}
箹锭⒈辈孓 2025-01-02 07:32:10

最简单的方法(但不会替换选定的文本)是使用 insertText: 方法:

[textView insertText:@"some text you want to insert"];

UITextView 符合UITextInput 本身符合 UIKeyInput

The simplest way (but it won't replace selected text) is to use the insertText: method:

[textView insertText:@"some text you want to insert"];

UITextView conforms to UITextInput which itself conforms to UIKeyInput.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文