如何扩展NSTextStorage? [英] How to extend NSTextStorage?
问题描述
根据文档,我尝试子类化NSTextStorage并在文本视图中使用它:
According to documentation, I try to subclass NSTextStorage and use it in text view:
/*
NSTextStorage is a semi-abstract subclass of NSMutableAttributedString. It
implements change management (beginEditing/endEditing), verification of
attributes, delegate handling, and layout management notification. The one
aspect it does not implement is the actual attributed string storage --- this is
left up to the subclassers, which need to override the two
NSMutableAttributedString primitives:
- (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)str;
- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range;
*/
所以我试着使用delegate来处理所有需要的具有相同功能的事件:
So I try to use delegate, to handle all needed events with same functionality:
@implementation MyTextStorage
- (id) init {
self = [super init];
if (self != nil) {
storage = [[NSMutableAttributedString alloc] init];
}
return self;
}
- (void) dealloc {
[storage release];
[super dealloc];
}
- (NSString *) string {
return [storage string];
}
- (void) replaceCharactersInRange:(NSRange)range withString:(NSString *)str {
[storage replaceCharactersInRange:range withString:str];
}
- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range {
[storage setAttributes:attrs range:range];
}
@end
无论我使用什么作为委托:NSTextStorage或NSMutableAttributedString,结果是一样的:
It is no matter what I'm use as delegate: NSTextStorage or NSMutableAttributedString, The result is the same:
引发了一个未捕获的异常
* NSRunStorage,_NSBlockNumberForIndex():index(18446744073709551615)超出数组
bounds(0)
* 由于未捕获异常'NSRangeException'终止应用程序,原因:
'*** NSRunStorage,
_NSBlockNumberForIndex():index(18446744073709551615)超出数组
bounds(0)'
An uncaught exception was raised * NSRunStorage, _NSBlockNumberForIndex(): index (18446744073709551615) beyond array bounds (0) * Terminating app due to uncaught exception 'NSRangeException', reason: '*** NSRunStorage, _NSBlockNumberForIndex(): index (18446744073709551615) beyond array bounds (0)'
堆栈跟踪:
0 CoreFoundation 0x00007fff840cd7b4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x00007fff885390f3 objc_exception_throw + 45
2 CoreFoundation 0x00007fff840cd5d7 +[NSException raise:format:arguments:] + 103
3 CoreFoundation 0x00007fff840cd564 +[NSException raise:format:] + 148
4 AppKit 0x00007fff86cc6288 _NSBlockNumberForIndex + 86
5 AppKit 0x00007fff86cc71d5 -[NSLayoutManager textContainerForGlyphAtIndex:effectiveRange:] + 364
6 AppKit 0x00007fff86d1f121 -[NSTextView(NSSharing) didChangeText] + 340
7 AppKit 0x00007fff86d44b68 -[NSTextView insertText:replacementRange:] + 2763
8 CocoaCalculator 0x0000000100002312 -[CalcTextView insertText:] + 65
9 CocoaCalculator 0x00000001000027ac -[CalcTextContainer initWithFrame:] + 1176
10 AppKit 0x00007fff86c23d44 -[NSCustomView nibInstantiate] + 646
11 AppKit 0x00007fff86b7be17 -[NSIBObjectData instantiateObject:] + 259
12 AppKit 0x00007fff86b7b202 -[NSIBObjectData nibInstantiateWithOwner:topLevelObjects:] + 336
13 AppKit 0x00007fff86b7988d loadNib + 226
14 AppKit 0x00007fff86b78d9a +[NSBundle(NSNibLoading) _loadNibFile:nameTable:withZone:ownerBundle:]
尝试呼叫时终止开始
[textView insertText:@"123"];
但是在标准的NSTextStorage版本中都能正常工作。
But in standard NSTextStorage version all works properly.
我需要做什么来扩展这个类?
What I need to do to extend this class ?
推荐答案
除了需要实现的NSAttributedString基本方法,我相信你还需要调用 c:replaceCharactersInRange:withString:
和 setAttributes:range:
。
In addition to the NSAttributedString primitive methods that need implemented, I believe you also need to make a call to edited:range:changeInLength:
inside your overrides of replaceCharactersInRange:withString:
and setAttributes:range:
respectively.
如下:
- (void) replaceCharactersInRange:(NSRange)range withString:(NSString *)str {
[storage replaceCharactersInRange:range withString:str];
[self edited:NSTextStorageEditedCharacters range:range changeInLength:[str length] - range.length];
}
- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range {
[storage setAttributes:attrs range:range];
[self edited:NSTextStorageEditedAttributes range:range changeInLength:0];
}
这篇关于如何扩展NSTextStorage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!