1

현재 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 당신이 정말 wiling 경우

// 
// 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

그것 : P, 나는 GNUStep과 같은 프로젝트를 살펴볼 것을 제안합니다.

해당 구현 (NSAttributedString.m) here을보고 official page에서 프로젝트를 다운로드 할 수 있습니다.

자, 내가 물어볼지도 모르는 부분은 여기에 있습니다.이 클래스를 작성하는 것이 쉬운 부분이지만 어떻게 "유용하게"만들 계획입니까? 사용 장소와 방법은 무엇입니까?

+0

안녕하세요. 난 이미 GNUStep에 대해 알고, NSAttributedString의 구현을 살펴볼 것이지만, 약간 복잡하고 나는 stackoverflow에 내 문제에 대해 물어 보려고 enlighting 것이라고 생각했다. 제가하고있는 일은 iOS 용 IDE를 작성하는 것입니다. 이를 위해서는 구문 강조 기능을 쉽게 사용할 수있는 텍스트 편집기가 필요합니다. 이를 위해 사용자 정의 UIWebView를 사용하고 HTML로 변환하여 해당 속성 문자열을 처리합니다. 관심이 있으시면 언제든지 이메일을 보내주십시오. developerFE2 (at) gmail.com – v1Axvw

+0

Hello @ Ief2! 아, 그 일이 더 분명해집니다. NSAttributedString이 받아 들여지는 곳에서 사용하기가 어려울 것입니다. 제안 해 주셔서 감사 드리며,이 코딩의 성공을 기원합니다. – sidyll

+0

예,'-initWithNSAttributedString :'및'-NSAttributedString'과 같은 메소드가 있어야합니다. 그러나 그것들은 나중에 문제가됩니다. – v1Axvw