简体   繁体   English

在位置Objective-C处插入字符串

[英]Insert string at location Objective-C

I'm making an app the has 'hotkeys' in it and when you tap the hotkey it should insert a character at the location you are typing at. 我正在制作一个应用程序,其中包含“热键”,当您点击热键时,它应该在您键入的位置插入一个字符。 I'm using a UITextView with editing on. 我正在使用UITextView进行编辑。

在此输入图像描述

What I want do do is insert the text right after the blue cursor. 我想你做的是,蓝色光标之后插入文本。
Is this possible? 这可能吗?

你想使用insertText:方法UITextView ,这是在其实施的声明UIKeyInput (这是一个超级协议协议UITextInput ,这UITextView 。器具)

You can get the location of the cursor by using 您可以使用获取光标的位置

int position = txtView.selectedRange.location;

Then you will need to get the string from the text view and insert your own string 然后,您需要从文本视图中获取字符串并插入自己的字符串

NSMutableString *str = [txtView.text mutableCopy];
[str insertString:@"YourString" atIndex:position];
txtView.text = str;

This may scroll the text view and lose your current cursor, if you dont want to lose it, you will need to subclass UITextView and do some work inside the drawRect 这可能会滚动文本视图并丢失当前光标,如果你不想丢失它,你需要继承UITextView并在drawRect中做一些工作

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

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