1

在 NSTextStorage 中,我在当前指针位置插入时间字符串,如下所示:

            NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString: @"00:00:00"];
            [attrString autorelease];
            int pos = [self selectedRange].location;
            [[self textStorage] insertAttributedString: attrString atIndex:pos];

到目前为止一切顺利,效果很好。但现在我想将指针定位在下一行的开头。显然这是在下一个返回字符之后。

现在如何找到下一个返回字符textStorage并将指针定位在那里?我没有在网上找到任何关于这个任务的提示。请帮忙 ...

4

1 回答 1

0

您不会在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 中输入,未经测试,只是为了展示基本方法。

于 2015-12-01T08:39:33.477 回答