首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何扩展NSTextStorage?

如何扩展NSTextStorage?
EN

Stack Overflow用户
提问于 2011-05-11 07:12:10
回答 2查看 2.2K关注 0票数 11

根据文档,我尝试将NSTextStorage子类并在文本视图中使用:

代码语言:javascript
复制
/*
 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;

*/

因此,我尝试使用委托来处理所有具有相同功能的所需事件:

代码语言:javascript
复制
@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,结果都是一样的:

未指明的异常被引发* NSRunStorage,_NSBlockNumberForIndex():索引(18446744073709551615)超出数组边界(0) *终止应用程序,原因:‘* NSRunStorage,_NSBlockNumberForIndex():索引(18446744073709551615)超出数组界限(0)’。

堆栈跟踪:

代码语言:javascript
复制
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:]

当我试图打电话时,终止开始

代码语言:javascript
复制
[textView insertText:@"123"];

但是在标准的NSTextStorage版本中,所有这些都能正常工作。

我需要做些什么来扩展这门课?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-05-12 03:38:08

除了需要实现的NSAttributedString原语方法之外,我相信您还需要在重写replaceCharactersInRange:withString:setAttributes:range:的过程中调用edited:range:changeInLength:

类似于:

代码语言:javascript
复制
- (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];
}
票数 11
EN

Stack Overflow用户

发布于 2011-05-11 23:37:16

NSTextStorage文档在这方面有点模糊。您在问题中引用的关于只需要覆盖2个NSMutableAttributedString原语的注释意味着您不必覆盖其他NSMutableAttributedString原语。

您仍然必须重写NSAttributedString超类的原语。这意味着您需要实现-attributesAtIndex:effectiveRange:-length-string。我注意到您确实包括了-string的一个实现,所以剩下的两个应该这样做。

如果你加上:

代码语言:javascript
复制
- (NSUInteger)length
{
     return [storage length];
}

- (NSDictionary *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range
{
      return [storage attributesAtIndex:location effectiveRange:range];
}

..。希望它能解决这个问题。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5960550

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档