我有两个带有共享自定义NSTextStorage子类的NSTextView。文本视图反映了彼此的内容。这一切正常,直到我按回车键。然后第二个textview开始滞后或甚至不显示任何新的输入。无论我输入哪种文本视图,都会发生这种情况。
如何强制第二个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];
}