将 NSString 转换为 UIColor [英] Convert NSString to UIColor
问题描述
我有一个充满 NSString 的数组,我需要使用 NSString 来更改属性文本的颜色.我将原始 NSString 分成一个包含带有颜色的单个 NSString 的数组,但我被困在这里.数组中的字符串是 [UIColor orangecolor].我试过了,但它返回 null.
I have an array full of NSStrings and I need to use an NSString to change the color on an attributed text. I seperated the original NSString into an array containing the single NSString with the color but I am stuck here. The string from the array is [UIColor orangecolor]. I have tried this but it returns null.
SEL blackSel = NSSelectorFromString(@"blackColor");
UIColor* tColor = nil;
if ([UIColor respondsToSelector: blackSel])
tColor = [UIColor performSelector:blackSel];
推荐答案
我编写了一个自定义的 NSValueTransformer
来允许我将 NSColor
转换为 NSString
>(然后返回)用于存储到 NSUserDefaults
中,因为我不喜欢使用字节数组存储颜色,这是默认行为.如果您在 IB 中设置自定义转换器,它也适用于 KVO.您当然也可以调用代码中的静态方法来执行转换.
I wrote a custom NSValueTransformer
to allow me to translate NSColor
to NSString
(and back) for storage into NSUserDefaults
as I didn't like the colours being stored using a byte array, which is the default behaviour. It also works across KVO, if you set the custom-transformer within IB. You can of course invoke the static methods in code to perform the transformation as well.
为 UIColor
重新设计它不应该是很多工作:
It shouldn't be a lot of work to rework this for UIColor
:
StringColourTransformer.h:
StringColourTransformer.h:
#import <Cocoa/Cocoa.h>
@interface StringColourTransformer : NSValueTransformer
+ (NSString *)toString:(NSColor *)value;
+ (NSColor *)fromString:(NSString *)value;
@end
StringColourTransformer.m:
StringColourTransformer.m:
#import "StringColourTransformer.h"
@implementation StringColourTransformer
+ (NSString *)toString:(NSColor *)value
{
StringColourTransformer *transformer = [[StringColourTransformer alloc] init];
NSString *str = [transformer reverseTransformedValue:value];
return str;
}
+ (NSColor *)fromString:(NSString *)value
{
StringColourTransformer *transformer = [[StringColourTransformer alloc] init];
NSColor *color = (NSColor *)[transformer transformedValue:value];
return color;
}
+ (Class)transformedValueClass
{
return [NSString class];
}
+ (BOOL)allowReverseTransformation
{
return YES;
}
- (id)transformedValue:(id)value
{
CGFloat r = 0.0, g = 0.0, b = 0.0, a = 1.0;
// Only NSString classes are reverse-transformed
if ([value isKindOfClass:[NSString class]])
{
NSString *stringValue = (NSString *)value;
sscanf([stringValue UTF8String],
#ifdef __x86_64
"%lf %lf %lf %lf",
#else
"%f %f %f %f",
#endif
&r, &g, &b, &a);
}
return [NSColor colorWithCalibratedRed:r green:g blue:b alpha:a];
}
- (id)reverseTransformedValue:(id)value
{
CGFloat r = 0.0, g = 0.0, b = 0.0, a = 1.0;
// Only NSColor classes are transformed
if ([value isKindOfClass:[NSColor class]])
{
NSColor *colourValue = (NSColor *)value;
NSColor *converted = [colourValue colorUsingColorSpaceName:@"NSCalibratedRGBColorSpace"];
[converted getRed:&r green:&g blue:&b alpha:&a];
}
return [NSString stringWithFormat:@"%.3f %.3f %.3f %.3f", r, g, b, a];
}
@end
这篇关于将 NSString 转换为 UIColor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!