[Cocoa] NSValueTransformer subclass: StringAttributes

Again, Matt Legend Gemmell introduces Setting the text color of an NSButton. I made its NSValueTransformer subclass version, which works with Cocoa Bindings.

Header,

#import <cocoa/Cocoa.h>
 
@interface TTStringAttributesValueTransformer : NSValueTransformer {
    NSDictionary *_attributes;
}
- (id) initWithColor:(NSColor *)color;
- (id) initWithAttributes:(NSDictionary *)attributes;
@end

Implementation,

#import <cocoa/Cocoa.h>
 
 
#import "TTStringAttributesValueTransformer.h"
 
 
@implementation TTStringAttributesValueTransformer
 
 
- (id) initWithColor:(NSColor *)color
{
    self = [super init];
    if (self != nil) {
        _attributes = [[NSDictionary dictionaryWithObjectsAndKeys:
                       color, NSForegroundColorAttributeName, nil] retain];
    }
    return self;
}
 
- (id) initWithAttributes:(NSDictionary *)attributes
{
    self = [super init];
    if (self != nil) {
        _attributes = [attributes retain];
    }
    return self;
}
 
- (void) dealloc
{
    [_attributes release];
    [super dealloc];
}
 
+ (Class)transformedValueClass {
    return [NSAttributedString class];
}
 
+ (BOOL)allowsReverseTransformation {
	return NO;
}
 
- (id)transformedValue:(id)value {
	if(! value)
		return nil;
	if ([value isKindOfClass:[NSAttributedString class]]) {
        NSMutableAttributedString *attrTitle = [[NSMutableAttributedString alloc]  initWithAttributedString:value];
        NSRange range = NSMakeRange(0, [attrTitle length]);
        [attrTitle addAttributes:_attributes
                           range:range];
        [attrTitle fixAttributesInRange:range];
        return [attrTitle autorelease];
    }
	if ([value isKindOfClass:[NSString class]]) {
		return [[[NSAttributedString alloc] initWithString:value
                                                attributes:_attributes] autorelease];
    }
    if ([value respondsToSelector: @selector(stringValue)]) {
		return [[[NSAttributedString alloc] initWithString:[value stringValue]
                                                attributes:_attributes] autorelease];
    }
    return nil;
}
 
- (id)reverseTransformedValue:(id)value
{
    return nil;
}
 
@end

Usage,

// white color
[NSValueTransformer setValueTransformer: [[[TTStringAttributesValueTransformer alloc] initWithColor:[NSColor whiteColor]] autorelease] forName:@"TTColorWhite"];
 
// white color + centering
NSMutableParagraphStyle *paraStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paraStyle setAlignment:NSCenterTextAlignment];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                      [NSColor whiteColor], NSForegroundColorAttributeName,
                      paraStyle, NSParagraphStyleAttributeName,
                      nil];
[NSValueTransformer setValueTransformer: [[[TTStringAttrubuteValueTransformer alloc]
                                           initWithAttributes:dict] autorelease]
                                forName:@"TTColorWhiteAndCenter"];
Body
Comment me!