objective c - How to extend NSTextStorage? -


according documentation, try subclass nstextstorage , use in text view:

/*  nstextstorage semi-abstract subclass of nsmutableattributedstring.  implements change management (beginediting/endediting), verification of  attributes, delegate handling, , layout management notification. 1  aspect not implement actual attributed string storage ---  left subclassers, need override 2  nsmutableattributedstring primitives:   - (void)replacecharactersinrange:(nsrange)range withstring:(nsstring *)str;  - (void)setattributes:(nsdictionary *)attrs range:(nsrange)range;  */ 

so try use delegate, handle needed events 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 

it no matter i'm use delegate: nstextstorage or nsmutableattributedstring, result same:

an uncaught exception raised * nsrunstorage, _nsblocknumberforindex(): index (18446744073709551615) beyond array bounds (0) * terminating app due uncaught exception 'nsrangeexception', reason: '*** nsrunstorage, _nsblocknumberforindex(): index (18446744073709551615) beyond array bounds (0)'

stack trace:

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:] 

termination start when try call

[textview inserttext:@"123"]; 

but in standard nstextstorage version works properly.

what need extend class ?

in addition nsattributedstring primitive methods need implemented, believe need make call edited:range:changeinlength: inside overrides of replacecharactersinrange:withstring: , setattributes:range: respectively.

something like:

- (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]; } 

Comments

Popular posts from this blog

Cursor error with postgresql, pgpool and php -

delphi - ESC/P programming! -

c++ - error: use of deleted function -