Anon
Anon

Reputation: 623

Drawing backgroundcolor & border Vs Background PNGs

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.

Upvotes: 2

Views: 53

Answers (2)

iDeveloper
iDeveloper

Reputation: 627

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

Upvotes: 0

Huy Nghia
Huy Nghia

Reputation: 986

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

Upvotes: 1

Related Questions