• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Subclassing NSMutableAttributedString
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.

References: 
 >Subclassing NSMutableAttributedString (From: Arthur Clemens <email@hidden>)

  • Prev by Date: Re: Bochs - Free PC emulator
  • Next by Date: Threads and unintended message forwarding
  • Previous by thread: Subclassing NSMutableAttributedString
  • Next by thread: DateFormatter in outlineview cell....(again sorry!)
  • Index(es):
    • Date
    • Thread