Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting Colors for Buttons Instead of Images? #72

Open
vicc opened this issue Jul 17, 2014 · 2 comments
Open

Setting Colors for Buttons Instead of Images? #72

vicc opened this issue Jul 17, 2014 · 2 comments

Comments

@vicc
Copy link

vicc commented Jul 17, 2014

Is there a way to define the button using uicolors rather than enum's for images?

@juzooda
Copy link

juzooda commented Nov 25, 2014

That would be cool

@ghost
Copy link

ghost commented Feb 17, 2015

Just came across this project and wanting the same thing. As your posts are old, don't know if you still need the help, but in any case, heres what I did.

Added:

- (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;
}

Then for the buttons in buttonForItemIndex:

    switch (item.type) {
        case SIAlertViewButtonTypeCancel:
            normalImage = [self imageFromColor:[UIColor colorWithRed:0.271 green:0.271 blue:0.271 alpha:0.75]];
            highlightedImage = [self imageFromColor:[UIColor colorWithRed:0.271 green:0.271 blue:0.271 alpha:1]];

            [button setTitleColor:self.cancelButtonColor forState:UIControlStateNormal];
            [button setTitleColor:[self.cancelButtonColor colorWithAlphaComponent:0.8] forState:UIControlStateHighlighted];
            break;

        case SIAlertViewButtonTypeDestructive:
            normalImage = [self imageFromColor:[UIColor colorWithRed:0.678 green:0.161 blue:0.188 alpha:0.50]];
            highlightedImage = [self imageFromColor:[UIColor colorWithRed:0.678 green:0.161 blue:0.188 alpha:0.65]];

            [button setTitleColor:self.destructiveButtonColor forState:UIControlStateNormal];
            [button setTitleColor:[self.destructiveButtonColor colorWithAlphaComponent:0.8] forState:UIControlStateHighlighted];
            break;

        case SIAlertViewButtonTypeDefault:
        default:
            normalImage = [self imageFromColor:[UIColor colorWithRed:0.271 green:0.271 blue:0.271 alpha:0.75]];
            highlightedImage = [self imageFromColor:[UIColor colorWithRed:0.271 green:0.271 blue:0.271 alpha:1]];

            [button setTitleColor:self.buttonColor forState:UIControlStateNormal];
            [button setTitleColor:[self.buttonColor colorWithAlphaComponent:0.8] forState:UIControlStateHighlighted];
            break;
    }

Obviously defining corner radius would look ideal too:
button.layer.cornerRadius = 8;
button.clipsToBounds = YES;

Customizing font, title, word wrap etc to fit your styling needs, but that's the jist of what I did.

Hope this helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants