0

我想在光标当前所在的位置插入文本,所以我有以下代码

    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

我想在附加文本之前和之后留一个空格(嗨,朋友)。

那我应该怎么做呢?

4

2 回答 2

0

尝试类似:

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];
于 2012-04-30T06:36:32.007 回答
0

尝试这样的事情:

NSString *beginning = [self.myTextView.text substringToIndex:currentRange.location];
NSString *ending = [self.myTextView.text substringFromIndex:currentRange.location];
self.myTextView.text = [NSString stringWithFormat:@"%@% @% @",beginning, contentsToAdd, ending, nil];
于 2012-04-30T07:16:58.383 回答