Pregunta

Actualmente estoy tratando de implementar una cadena atribuida para su uso en el desarrollo de iOS. Se acerca de NSAttributedString, pero pensé que sería divertido implementarlo yo mismo.

Tengo la interfaz básica y los métodos de asignación, pero estoy atascado sobre cómo implementar la parte del atributo: d. Primero pensé en guardar todos los atributos (un NSDictionary) para cada personaje de la cadena, pero no sé si ese es el mejor método para alcanzar mi objetivo.

¿Alguna idea?

PD: A continuación puedes encontrar lo que ya tengo:

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

No hay solución correcta

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top