2

I want specific RGB hex color for most of the buttons for my iPad Application.

What I have done is: I have implemented UIButton Subclass in my project which emulates the required background color, text color & border for buttons - and then I use it everywhere in my project where I need UIButton.

My UI/UX designer do not provide PNGs instead he insist that I imitate the same color web site buttons are having with Hex codes. I am more of a background PNGs fan.

Now, relation of this question to programming as you might wonder is that, I am accessing .layer.borderWidth & .layer.borderColor property in Subclass which eventually gets used everywhere.

So, is this approach doing any good to the Application instead of using background PNGs?

Note (if relevant): All the buttons are of same size.

Anon
  • 623
  • 3
  • 10

2 Answers2

1

you can make UIImage from color by yourself and set yourButton image:

+ (UIImage *)imageFromColor:(UIColor *)color {
    CGRect rect = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

you also need convert HEX RGB color codes to UIColor

Community
  • 1
  • 1
Huy Nghia
  • 996
  • 8
  • 22
  • Thank you for the answer, however I want to know the overhead (in terms of memory) it would be causing to the Application. I mean, I can use the RGB directly to imitate the color OR can create image from Color. But, the question is it advisable? Or one should use background PNGs (by adding images to the resource folder.) – Anon Feb 20 '15 at 08:05
  • It's hard to say what better. I think if you not have much color you should use PNGs :) – Huy Nghia Feb 20 '15 at 08:12
  • Yes, It's just a single color, I am looking for a reason why it is no good. :) – Anon Feb 20 '15 at 09:29
0

This may help you.

- (unsigned int)intFromHexString:(NSString *)hexStr
{
@try {
    unsigned int hexInt = 0;
    NSScanner *scanner = [NSScanner scannerWithString:hexStr];
    [scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"#"]];
    [scanner scanHexInt:&hexInt];
    return hexInt;
}
@catch (NSException *exception) {

}
}



- (UIColor *)getUIColorObjectFromHexString:(NSString *)hexStr alpha:(CGFloat)alpha
{
unsigned int hexint = [self intFromHexString:hexStr];
UIColor *color =
[UIColor colorWithRed:((CGFloat) ((hexint & 0xFF0000) >> 16))/255
                green:((CGFloat) ((hexint & 0xFF00) >> 8))/255
                 blue:((CGFloat) (hexint & 0xFF))/255
                alpha:alpha];

return color;
}

This function will give you color from hex string which you can set in your button

iDeveloper
  • 607
  • 5
  • 25