現在、iOS開発で使用する属性付き文字列を実装しようとしています。私はNSAttributedString
について知っていますが、私はそれを自分で実装するのは楽しいと思いました。属性付き文字列インターフェイスの実装
私は基本的なインターフェイスと割り当て方法を持っていますが、私は属性部分を実装する方法についていません。私は最初に文字列内のすべての文字のすべての属性(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
こんにちは。私はすでにGNUStepについて知っていて、NSAttributedStringの実装を見ていますが、それはちょっと複雑ですし、平均的なstackoverflowに関する私の問題について尋ねることが可能になると思いました。私がやっていることは、iOS用の完全なIDEを書くことです。そのためには、簡単に構文ハイライトを行うことができるテキストエディタが必要です。そのために私はカスタムUIWebViewを使用しており、HTMLに変換することによってそれらの属性文字列を処理します。興味があれば、いつでも私に電子メールを送ることができます:developerFE2(at)gmail.com – v1Axvw
Hello @ Ief2!ああ、それは物事をより明確にします。 NSAttributedStringが受け入れられる場所でそれを使用するのは難しいでしょう。オファーをありがとう、私はあなたがこのコーディングで成功したことを願っています。 – sidyll
はい、 '-initWithNSAttributedString:'や '-NSAttributedString'のようなメソッドが必要です。しかし、それらは後で問題になる。 – v1Axvw