我有两个具有共享自定义 NSTextStorage 子类的 NSTextView。文本视图相互镜像。这工作正常,直到我按回车。然后第二个文本视图开始滞后,甚至不显示任何新输入。无论我输入哪个文本视图,都会发生这种情况。
如何强制第二个 textview 与第一个 textview 保持同步?
这是我的 NSTextStorage 子类。
@implementation MyTextStorage
{
NSTextStorage *_backingStore;
}
-(id) init
{
self = [super init];
if (self)
{
_backingStore = [[NSTextStorage alloc] initWithString:@""];
}
return self;
}
-(NSString *) string
{
return [_backingStore string];
}
-(NSDictionary *) attributesAtIndex:(NSUInteger) location
effectiveRange:(NSRangePointer) range
{
NSMutableDictionary *attributes = [[_backingStore attributesAtIndex:location
effectiveRange:range] mutableCopy];
if ([self displayMode] == MyTextStorageDisplayNormal)
{
[attributes setObject:[NSColor blackColor]
forKey:NSForegroundColorAttributeName];//remove syntax coloring
}
return attributes;
}
#pragma mark - editing
-(void) replaceCharactersInRange:(NSRange)range
withString:(NSString *)string
{
[_backingStore replaceCharactersInRange:range
withString:string];
[self edited:NSTextStorageEditedCharacters
range:range
changeInLength:[string length] - range.length];
}
-(void) setAttributes:(NSDictionary *) attributes
range:(NSRange) range
{
[_backingStore setAttributes:attributes
range:range];
[self edited:NSTextStorageEditedAttributes
range:range
changeInLength:0];
}