1

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

1 Answer 1

1

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?

Sign up to request clarification or add additional context in comments.

3 Comments

Hi. I already know about GNUStep, and I'm taking a look at the NSAttributedString's implementation, but it's a bit complicated and I thought it would be enlighting to ask about my problem on stackoverflow in the mean time. What I am doing is writing a full IDE for the iOS. For that I need a text editor in which I can easily do syntax higlighting. For that I'm using a custom UIWebView, and that one will handle those attribured strings by converting them to HTML. If you are interested, you can always send me an e-mail: developerief2(at)gmail.com
Hello @Ief2! Ah, that makes things more clear. It would be hard to use it in places the NSAttributedString is accepted. Thanks for the offer, and I wish you success in this coding
Yes, of couse it will have to have methods like -initWithNSAttributedString: and -NSAttributedString. But those are problems for later.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.