DotMac

プログラムに関する覚え書き等

Subcalssing text storage/NSTextStorageのサブクラスを作る

 NSTextStorageをサブクラス化するには、以下のようなメソッドを追加しておく必要がある。

(www.cocoadev.comより)




//#####################


- (id)init {

    self = [super init];

    if (self) {

m_attributedString = [[NSMutableAttributedString alloc] init];

    }

    

    return self;

}


- (void)dealloc

{

    [[NSNotificationCenter defaultCenter] removeObserver:self];

    [m_attributedString release];


    [super dealloc];

}





- (NSString *)string

{

    return [m_attributedString string];

}


- (NSDictionary *)attributesAtIndex:(unsigned)index effectiveRange:(NSRangePointer)aRange

{

    return [m_attributedString attributesAtIndex:index effectiveRange:aRange];

}


- (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)str

{

    [m_attributedString replaceCharactersInRange:range withString:str];

    

    int lengthChange = [str length] - range.length;

    [self edited:NSTextStorageEditedCharacters range:range changeInLength:lengthChange];

}


- (void)setAttributes:(NSDictionary *)attributes range:(NSRange)range

{

    [m_attributedString setAttributes:attributes range:range];

    [self edited:NSTextStorageEditedAttributes range:range changeInLength:0];

}




@end

 

Parting Words (著作権, 連絡先情報, etc.)