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

テキストを追加する前後に 1 つのスペースを残したいです (こんにちは友人)。

では、どうすればいいのでしょうか?

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 に答える