我有一个包含多行文字的NSTextView。调用-deleteToBeginningOfLine:
时,将删除整个字符串,而不是最后一行:
NSTextView *textView = [[NSTextView alloc] initWithFrame:NSZeroRect];
textView.string = @"foo\nbar";
[textView deleteToBeginningOfLine:nil];
NSAssert([textView.string isEqualToString:@"foo\n"], @""); // fail, string is "".
// Modifying the selected range doesn't help.
textView.string = @"foo\nbar";
textView.selectedRange = NSMakeRange(textView.string.length, 0);
[textView deleteToBeginningOfLine:nil];
NSAssert([textView.string isEqualToString:@"foo\n"], @""); // ditto.
// Using \r\n changes nothing.
textView.string = @"foo\r\nbar";
[textView deleteToBeginningOfLine:nil];
NSAssert([textView.string isEqualToString:@"foo\r\n"], @""); // ditto.
一旦视图在窗口中布局,它就会按预期工作。但我想在将视图添加到任何其他视图之前编辑字符串。我如何使这项工作?
答案 0 :(得分:0)
创建具有良好框架尺寸的文本视图。
NSTextView *textView = [[NSTextView alloc] initWithFrame:NSMakeRect(0, 0, 1000, 1000)];
textView.string = @"foo\nbar";
[textView deleteToBeginningOfLine:nil];
NSAssert([textView.string isEqualToString:@"foo\n"], @""); // ok.
// Don't make the frame too small.
NSTextView *textView = [[NSTextView alloc] initWithFrame:NSMakeRect(0, 0, 1, 1)];
textView.string = @"foo\nbar";
[textView deleteToBeginningOfLine:nil];
NSAssert([textView.string isEqualToString:@"foo\n"], @""); // fail, string is "foo\nba".