您不会在NSTextStorage
的 API 中找到设置光标的方法(更准确地说:长度为零的选择),因为它是一个存储,没有选择。选择是文本视图的属性。这是MVC的结果。只需检查一下:您可以有许多文本视图显示相同的文本。显然,每个人都需要自己的选择。
您要做的是获取下一段的位置(这比搜索要好\n
)并将其设置为文本视图的选择。
NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString: @"00:00:00"];
[attrString autorelease]; // You should *really* use ARC
int pos = [self selectedRange].location;
[[self textStorage] insertAttributedString: attrString atIndex:pos];
// Get the next paragraph
NSString *text = self.textStorage.string;
NSRange restOfStringRange = NSMakeRange( pos, [text length]-pos );
NSUInteger nextParagraphIndex;
// You have to look to the start of the next paragraph
[text getParagraphStart:&nextParagraphIndex end:NULL contentsEnd:NULL forRange:restOfStringRange];
NSTextView *view = self.textView; // Or where ever you get it from
view.selectedRange = NSMakeRange(nextParagraphIndex, 0);
在 Safari 中输入,未经测试,只是为了展示基本方法。