o9s
is a microblog
home archives search feed blogroll


Colors With RGBA Strings

This is a follow up to this post. I recently needed to do the same (convert a string to a UIColor) but this time with RGBA color strings.

+ (UIColor*)colorWithRGBAString:(NSString*)rgbaString {
    if (rgbaString == nil) {
        return nil;
    }
    
    rgbaString = [rgbaString lowercaseString];
    rgbaString = [rgbaString stringByReplacingOccurrencesOfString:@"rgba(" withString:@""];
    rgbaString = [rgbaString stringByReplacingOccurrencesOfString:@")" withString:@""];
    
    NSArray *components = [rgbaString componentsSeparatedByString:@","];
    
    if (components.count != 4) {
        return nil;
    }
    
    CGFloat red = [components[0] floatValue] / 255;
    CGFloat green = [components[1] floatValue] / 255;
    CGFloat blue = [components[2] floatValue] / 255;
    CGFloat alpha = [components[3] floatValue];
    
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}
Posted on 2018-04-12   #objc  






← Next post    ยท    Previous post →