2012-04-30 49 views
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]; 

    This works as below 
    Hi 
    HiFriend 

其实我想之前和附加文本后留下一个空间(喜的朋友)。

所以我该怎么去。 扶我起来,欢呼声

回答

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]; 
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];