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