实现属性字符串接口

我目前正在尝试实现一个用于iOS开发的属性字符串。我知道
NSAttributedString
,但我认为自己实施它会很有趣。 我有基本的接口和分配方法,但我坚持如何实现属性部分:D。我首先考虑为字符串中的每个字符保存所有属性(一个
NSDictionary
),但我不知道这是否是达到目标的最佳方法。 有什么想法吗? PS:下面你可以找到我已有的东西: 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
    
已邀请:
如果你真的愿意这样做:P,我建议看一下像GNUStep这样的项目。 您可以在此处查看其实现(NSAttributedString.m),并在官方页面中下载该项目。 现在,如果我可以问你一些问题,那就写下来了:写这个课程很容易,但是你打算如何使它“可用”,我的意思是在哪里使用以及如何使用?     

要回复问题请先登录注册