在光标所在的位置插入字符串 [英] Insert string where the cursor is
本文介绍了在光标所在的位置插入字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在光标当前所在的位置插入文本,所以我有以下代码
I would like to insert text where the cursor is currently, so I have the below code
NSString *contentsToAdd = [myData objectAtIndex:row];
NSMutableString *tfContent = [[NSMutableString alloc] initWithString:[self.myTextView text]];
// add content where the cursor was
[tfContent insertString:contentsToAdd atIndex:myCursorPosition.location];
[self.myTextView setText:tfContent];
[tfContent release];
它的工作原理如下
Hi
HiFriend
我想在添加文字前后添加一个空格(朋友).
I want to leave one space before and after appending the text (hi Friend).
那我该怎么办?
推荐答案
尝试类似的方法:
NSString *contentsToAdd = [myData objectAtIndex:row];
NSMutableString *tfContent = [[NSMutableString alloc] initWithString:[self.myTextView text]];
// add content where the cursor was
NSString *contentsToAddPadded = [NSString stringWithFormat:@" %@ ", contentsToAdd];
[tfContent insertString:contentsToAddPadded atIndex:myCursorPosition.location];
[self.myTextView setText:tfContent];
[tfContent release];
这篇关于在光标所在的位置插入字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文