Reputation: 3054
I'm currently trying to implement an attributed string for use in iOS development. I know about NSAttributedString
, but I thought it would be fun to implement it myself.
I have the basic interface and allocation methods, but I'm stuck on how to implement the attribute part :D. I first thought about saving all attributes (an NSDictionary
) for every single character in the string, but I don't know if that is the best method to reach my goal.
Any thoughts?
PS: Below you can find what I already have:
TXAttributedString.h
//
// TXAttributedString.h
// Texter
//
// Created by ief2 on 6/02/11.
//
//
#import <Foundation/Foundation.h>
#ifndef UNUSED
#define UNUSED __attribute__((unused))
#endif
#pragma mark -
#pragma mark Constants
enum {
kScriptAttributeSubscript = -1,
kScriptAttributeNone = 0,
kScriptAttributeSuperscript = 1
};
enum {
kTextDecorationNone = 0,
kTextDecorationUnderline = 1,
kTextDecorationOverline = 2,
kTextDecorationLineThrough = 3
};
#pragma mark -
#pragma mark Attribute Contents
UNUSED static const NSString *TXBackgroundColorAttribute = @"TXBackgroundColorAttribute";
/* UIColor, none */
UNUSED static const NSString *TXFontNameAttribute = @"TXFontNameAttribute";
/* NSString, Helvetica */
UNUSED static const NSString *TXFontSizeAttribute = @"TXFontSizeAttribute";
/* NSNumber (float), 12 */
UNUSED static const NSString *TXForegroundColorAttribute = @"TXFForegroundColorAttribute";
/* UIColor, black */
UNUSED static const NSString *TXLinkAttrubte = @"TXLinkAttrubte";
/* NSURL, none */
UNUSED static const NSString *TXScriptAttribute = @"TXScriptAttribute";
/* NSNumber, (int) kScriptAttributeNone */
UNUSED static const NSString *TXTextDecorationAttribute = @"TXTextDecorationAttribute";
/* NSNumber, (int) kTextDecorationNone */
#pragma mark -
#pragma mark Public Interface
@interface TXAttributedString : NSObject {
NSString *_string;
NSMutableDictionary *_attributes;
}
#pragma mark Init
- (id)initWithString:(NSString *)str;
- (id)initWithAttributedString:(TXAttributedString *)str;
#pragma mark Changing Attributes
- (void)setAttributes:(NSDictionary *)attr range:(NSRange)range;
- (void)addAttribute:(NSString *)key value:(id)value range:(NSRange)range;
- (void)addAttributes:(NSDictionary *)attr range:(NSRange)range;
- (void)removeAttribute:(NSString *)key range:(NSRange)range;
- (void)removeAttributesInRange:(NSRange)range;
#pragma mark Editing Contents
- (void)replaceCharactersInRange:(NSRange)range
withAttributedString:(TXAttributedString *)str;
- (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)str;
- (void)appendAttributedString:(TXAttributedString *)str;
- (void)insertAttributedString:(TXAttributedString *)str atIndex:(NSUInteger)i;
- (void)deleteCharactersInRange:(NSRange)range;
#pragma mark Retreiving Attributes
- (NSDictionary *)attributesAtIndex:(NSUInteger)i
effectiveRange:(NSRange *)range;
#pragma mark Substrings
- (TXAttributedString *)attributedSubstringInRange:(NSRange)range;
#pragma mark Comparing
- (BOOL)isEqualToAttributedString:(TXAttributedString *)str;
#pragma mark Info
- (NSUInteger)length;
#pragma mark Properties
@property (nonatomic, retain) NSString *string;
@property (nonatomic, retain, readonly) NSDictionary *attributes;
@end
TXAttributedString.m
//
// TXAttributedString.m
// Texter
//
// Created by ief2 on 6/02/11.
//
#import "TXAttributedString.h"
#pragma mark -
#pragma mark Public Interface
@interface TXAttributedString (PrivateMethod)
#pragma mark Properties
@property (nonatomic, retain) NSDictionary *attributes;
@end
#pragma mark -
#pragma mark Implementation
@implementation TXAttributedString
#pragma mark Init and Dealloc
- (id)initWithString:(NSString *)str {
self = [super init];
if(self != nil) {
self.string = str;
}
return self;
}
- (void)dealloc {
[_string dealloc];
[_attributes release];
[super dealloc];
}
#pragma mark Properties
@synthesize string=_string;
@synthesize attributes=_attributes;
- (void)setAttributes:(NSDictionary *)d {
if(d != _attributes) {
[_attributes release];
_attributes = [d mutableCopy];
}
}
- (NSDictionary *)attributes {
return [NSDictionary dictionaryWithDictionary:_attributes];
}
@end
Upvotes: 1
Views: 784
Reputation: 59297
If you are really wiling to do it :P , I'd suggesting having a look in a project like GNUStep.
You can view their implementation (NSAttributedString.m) here, and download the project in the official page.
Now, If I may ask you something, here it goes: writing this class is the easy part, but how do you plan to make it "usable", I mean where to use and how?
Upvotes: 1