Re: Subclassing NSMutableAttributedString
Re: Subclassing NSMutableAttributedString
- Subject: Re: Subclassing NSMutableAttributedString
- From: Arthur Clemens <email@hidden>
- Date: Wed, 2 Oct 2002 14:28:38 +0200
OK, I can answer my own question now :-)
With some more digging in the archives (thanks Mamasam!) I found a
letter from Ali Ozer: "Re: subclassing NSTextStorage answered".
Since my question boiled down to "what methods do I need to implement
for a subclass of NSMutableAttributedString and how to implement them",
and since NSTextStorage is a subclass of NSMutableAttributedString, I
could use the methods in the letter directly.
Here is my implementation (please correct me if I miss something):
// header file
#import <Foundation/Foundation.h>
#import <AppKit/NSAttributedString.h>
@interface MyMutableAttributedString : NSMutableAttributedString {
NSMutableAttributedString *_contents;
}
// Methods to be implemented in each subclass of
NSMutableAttributedString:
- (id)init;
- (id)initWithAttributedString:(NSAttributedString *)attributedString;
- (NSString *)string;
- (NSDictionary *)attributesAtIndex:(unsigned)location
effectiveRange:(NSRange *)range;
- (void)replaceCharactersInRange:(NSRange)range
withString:(NSString *)string;
- (void)setAttributes:(NSDictionary *)attributes
range:(NSRange)range;
- (void)dealloc;
@end
// implementation file
#import "MyMutableAttributedString.h"
@implementation MyMutableAttributedString
//
------------------------------------------------------------------------
--------------------------------------------------
// Methods to be implemented in each subclass of
NSMutableAttributedString
//
------------------------------------------------------------------------
--------------------------------------------------
- init
{
return [self initWithAttributedString:nil];
}
- (id)initWithAttributedString:(NSAttributedString *)attributedString
{
if (self = [super init]) {
_contents = attributedString ? [attributedString mutableCopy] :
[[NSMutableAttributedString alloc] init];
}
return self;
}
- (NSString *)string
{
return [_contents string];
}
- (NSDictionary *)attributesAtIndex:(unsigned)location
effectiveRange:(NSRange *)range
{
return [_contents attributesAtIndex:location effectiveRange:range];
}
- (void)replaceCharactersInRange:(NSRange)range
withString:(NSString *)string
{
[_contents replaceCharactersInRange:range
withString:string];
}
- (void)setAttributes:(NSDictionary *)attributes
range:(NSRange)range
{
[_contents setAttributes:attributes range:range];
}
- (void)dealloc
{
[_contents release];
[super dealloc];
}
Arthur Clemens
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.