【问题标题】:Implementing an attributed string interface实现属性字符串接口
【发布时间】:2011-02-06 19:20:47
【问题描述】:

我目前正在尝试实现一个用于 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

【问题讨论】:

    标签: objective-c implementation foundation nsattributedstring


    【解决方案1】:

    如果你真的愿意这样做 :P ,我建议你看看像 GNUStep 这样的项目。

    您可以查看他们的实现(NSAttributedString.m)here,并在official page 中下载项目。

    现在,如果我可以问你一些事情,那就是:编写这个类是很容易的部分,但是你打算如何使它“可用”,我的意思是在哪里使用以及如何使用?

    【讨论】:

    • 嗨。我已经了解 GNUStep,并且正在研究 NSAttributedString 的实现,但这有点复杂,我认为同时询问我在 stackoverflow 上的问题会很有启发性。我正在做的是为 iOS 编写一个完整的 IDE。为此,我需要一个文本编辑器,我可以在其中轻松地进行语法高亮显示。为此,我使用了一个自定义 UIWebView,它会通过将这些属性字符串转换为 HTML 来处理它们。如果您有兴趣,可以随时给我发电子邮件:developief2(at)gmail.com
    • 你好@Ief2!啊,这样就更清楚了。在接受 NSAttributedString 的地方很难使用它。感谢您的提议,祝您在此编码中取得成功
    • 是的,当然它必须有像-initWithNSAttributedString:-NSAttributedString 这样的方法。但这些都是以后的问题。
    猜你喜欢
    • 2017-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-13
    • 2012-10-26
    • 2016-01-28
    相关资源
    最近更新 更多