Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert NSString to UIColor

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];
like image 851
user2076774 Avatar asked Nov 23 '25 08:11

user2076774


2 Answers

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.

It shouldn't be a lot of work to rework this for UIColor:

StringColourTransformer.h:

#import <Cocoa/Cocoa.h>

@interface StringColourTransformer : NSValueTransformer

+ (NSString *)toString:(NSColor *)value;
+ (NSColor *)fromString:(NSString *)value;

@end

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
like image 190
trojanfoe Avatar answered Nov 28 '25 17:11

trojanfoe


Use this method,Will convert colorname to the object UIColor

-(UIColor *)giveColorfromStringColor:(NSString *)colorname
{
    SEL labelColor = NSSelectorFromString(colorname);
    UIColor *color = [UIColor performSelector:labelColor];
    return color;
}
like image 44
Lithu T.V Avatar answered Nov 28 '25 16:11

Lithu T.V



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!