1

私は現在、iOS開発で使用するために属性付き文字列を実装しようとしています。知ってNSAttributedStringいますが、自分で実装するのも楽しいと思いました。

私は基本的なインターフェースと割り当て方法を持っていますが、属性部分:Dを実装する方法に固執しています。最初に、文字列内のすべての文字のすべての属性(an)を保存することを考え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
4

1 に答える 1

1

もしあなたが本当にそれをやりたいのなら:P、GNUStepのようなプロジェクトを見てみることをお勧めします。

ここでそれらの実装(NSAttributedString.m)を表示し、公式ページでプロジェクトをダウンロードできます。

さて、私があなたに何かを尋ねるかもしれませんが、ここにそれがあります:このクラスを書くことは簡単な部分ですが、どのようにそれを「使用可能」にする予定ですか、つまりどこでどのように使用するのですか?

于 2011-02-06T20:36:38.783 に答える