Drawing backgroundcolor & border Vs Background PNGs

≡放荡痞女 提交于 2020-01-04 11:09:14

问题


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.


回答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




回答2:


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



来源:https://stackoverflow.com/questions/28623154/drawing-backgroundcolor-border-vs-background-pngs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!