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

Added ability to change button colors via Core Graphics. #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions SIAlertView/SIAlertItem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// SIAlertItem.h
// SIAlertView
//
//
// Created by Kevin Cao on 5/30/13.
// Core Graphics integration by Christopher Constable.
// Copyright (c) 2013年 Sumi Interactive. All rights reserved.
//

#import <Foundation/Foundation.h>

@class SIAlertView;

typedef NS_ENUM(NSInteger, SIAlertViewButtonType) {
SIAlertViewButtonTypeDefault = 0,
SIAlertViewButtonTypeDestructive,
SIAlertViewButtonTypeCancel
};

typedef void(^SIAlertViewHandler)(SIAlertView *alertView);

@interface SIAlertItem : NSObject

@property (nonatomic, copy) NSString *title;
@property (nonatomic, strong) UIColor *buttonColor;
@property (nonatomic, assign) SIAlertViewButtonType type;
@property (nonatomic, copy) SIAlertViewHandler action;

- (UIImage *)imageForButton;
- (UIImage *)imageForButtonHighlighted;

@end
99 changes: 99 additions & 0 deletions SIAlertView/SIAlertItem.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//
// SIAlertItem.m
// SIAlertView
//
//
// Created by Kevin Cao on 5/30/13.
// Core Graphics integration by Christopher Constable.
// Copyright (c) 2013年 Sumi Interactive. All rights reserved.
//

#import "SIAlertItem.h"

@interface SIAlertItem ()

@end

@implementation UIColor (LightAndDark)

- (UIColor *)lighterColor
{
float h, s, b, a;
if ([self getHue:&h saturation:&s brightness:&b alpha:&a])
return [UIColor colorWithHue:h
saturation:s
brightness:MIN(b * 1.1, 1.0)
alpha:a];
return nil;
}

- (UIColor *)darkerColor
{
float h, s, b, a;
if ([self getHue:&h saturation:&s brightness:&b alpha:&a])
return [UIColor colorWithHue:h
saturation:s
brightness:b * 0.9
alpha:a];
return nil;
}

@end

@implementation SIAlertItem

- (UIImage *)imageForButton
{
CGSize buttonSize = CGSizeMake(9, 9);
UIGraphicsBeginImageContext(buttonSize);
CGContextRef context = UIGraphicsGetCurrentContext();

//// Color Declarations
UIColor* color = [self.buttonColor darkerColor];
UIColor* color2 = self.buttonColor;

//// Rounded Rectangle Drawing
UIBezierPath* roundedRectanglePath = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(0, 0, 9, 9) cornerRadius: 5];
[color2 setFill];
[roundedRectanglePath fill];
[color setStroke];
roundedRectanglePath.lineWidth = 1;
[roundedRectanglePath stroke];

CGImageRef imgRef = CGBitmapContextCreateImage(context);
UIImage* image = [UIImage imageWithCGImage:imgRef];
CGImageRelease(imgRef);

UIGraphicsEndImageContext();

return image;
}

- (UIImage *)imageForButtonHighlighted
{
CGSize buttonSize = CGSizeMake(9, 9);
UIGraphicsBeginImageContext(buttonSize);
CGContextRef context = UIGraphicsGetCurrentContext();

//// Color Declarations
UIColor* color = [[self.buttonColor darkerColor] darkerColor];
UIColor* color2 = [self.buttonColor darkerColor];

//// Rounded Rectangle Drawing
UIBezierPath* roundedRectanglePath = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(0, 0, 9, 9) cornerRadius: 5];
[color2 setFill];
[roundedRectanglePath fill];
[color setStroke];
roundedRectanglePath.lineWidth = 1;
[roundedRectanglePath stroke];

CGImageRef imgRef = CGBitmapContextCreateImage(context);
UIImage* image = [UIImage imageWithCGImage:imgRef];
CGImageRelease(imgRef);

UIGraphicsEndImageContext();

return image;
}

@end
17 changes: 10 additions & 7 deletions SIAlertView/SIAlertView.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,13 @@
//

#import <UIKit/UIKit.h>
#import "SIAlertItem.h"

extern NSString *const SIAlertViewWillShowNotification;
extern NSString *const SIAlertViewDidShowNotification;
extern NSString *const SIAlertViewWillDismissNotification;
extern NSString *const SIAlertViewDidDismissNotification;

typedef NS_ENUM(NSInteger, SIAlertViewButtonType) {
SIAlertViewButtonTypeDefault = 0,
SIAlertViewButtonTypeDestructive,
SIAlertViewButtonTypeCancel
};

typedef NS_ENUM(NSInteger, SIAlertViewBackgroundStyle) {
SIAlertViewBackgroundStyleGradient = 0,
SIAlertViewBackgroundStyleSolid,
Expand All @@ -33,13 +28,15 @@ typedef NS_ENUM(NSInteger, SIAlertViewTransitionStyle) {
};

@class SIAlertView;
typedef void(^SIAlertViewHandler)(SIAlertView *alertView);

@interface SIAlertView : UIView

@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *message;

/** For button drawing. To preserve backwards compatibility, this defaults to 0 (NO). */
@property (nonatomic) NSInteger useCoreGraphics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

@property (nonatomic, assign) SIAlertViewTransitionStyle transitionStyle; // default is SIAlertViewTransitionStyleSlideFromBottom
@property (nonatomic, assign) SIAlertViewBackgroundStyle backgroundStyle; // default is SIAlertViewButtonTypeGradient

Expand All @@ -54,6 +51,12 @@ typedef void(^SIAlertViewHandler)(SIAlertView *alertView);
@property (nonatomic, strong) UIColor *messageColor NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
@property (nonatomic, strong) UIFont *titleFont NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
@property (nonatomic, strong) UIFont *messageFont NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
@property (nonatomic, strong) UIColor *defaultButtonFontColor NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
@property (nonatomic, strong) UIColor *defaultButtonColor NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
@property (nonatomic, strong) UIColor *destructiveButtonFontColor NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
@property (nonatomic, strong) UIColor *destructiveButtonColor NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
@property (nonatomic, strong) UIColor *cancelButtonFontColor NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
@property (nonatomic, strong) UIColor *cancelButtonColor NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
@property (nonatomic, strong) UIFont *buttonFont NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
@property (nonatomic, assign) CGFloat cornerRadius NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; // default is 2.0
@property (nonatomic, assign) CGFloat shadowRadius NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; // default is 8.0
Expand Down
148 changes: 129 additions & 19 deletions SIAlertView/SIAlertView.m
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ - (id)initWithFrame:(CGRect)frame andStyle:(SIAlertViewBackgroundStyle)style
self.opaque = NO;
self.windowLevel = UIWindowLevelAlert;
}

return self;
}

Expand Down Expand Up @@ -117,20 +118,6 @@ - (void)drawRect:(CGRect)rect

@end

#pragma mark - SIAlertItem

@interface SIAlertItem : NSObject

@property (nonatomic, copy) NSString *title;
@property (nonatomic, assign) SIAlertViewButtonType type;
@property (nonatomic, copy) SIAlertViewHandler action;

@end

@implementation SIAlertItem

@end

#pragma mark - SIAlertViewController

@interface SIAlertViewController : UIViewController
Expand Down Expand Up @@ -176,6 +163,13 @@ + (void)initialize
appearance.messageColor = [UIColor darkGrayColor];
appearance.titleFont = [UIFont boldSystemFontOfSize:20];
appearance.messageFont = [UIFont systemFontOfSize:16];
appearance.useCoreGraphics = 0;
appearance.defaultButtonFontColor = [UIColor darkGrayColor];
appearance.defaultButtonColor = [UIColor colorWithRed:231.0 / 255.0 green:231.0 / 255.0 blue:231.0 / 255.0 alpha:1.0];
appearance.destructiveButtonFontColor = [UIColor whiteColor];
appearance.destructiveButtonColor = [UIColor colorWithRed:240.0 / 255.0 green:70.0 / 255.0 blue:62.0 / 255.0 alpha:1.0];
appearance.cancelButtonFontColor = [UIColor darkGrayColor];
appearance.cancelButtonColor = [UIColor colorWithRed:205.0 / 255.0 green:205.0 / 255.0 blue:205.0 / 255.0 alpha:1.0];
appearance.buttonFont = [UIFont systemFontOfSize:[UIFont buttonFontSize]];
appearance.cornerRadius = 2;
appearance.shadowRadius = 8;
Expand All @@ -193,7 +187,19 @@ - (id)initWithTitle:(NSString *)title andMessage:(NSString *)message
_title = title;
_message = message;
self.items = [[NSMutableArray alloc] init];

// Set the default button colors.
SIAlertView *appearance = [SIAlertView appearance];
_defaultButtonFontColor = appearance.defaultButtonFontColor;
_defaultButtonColor = appearance.defaultButtonColor;
_destructiveButtonFontColor = appearance.destructiveButtonFontColor;
_destructiveButtonColor = appearance.destructiveButtonColor;
_cancelButtonFontColor = appearance.cancelButtonFontColor;
_cancelButtonColor = appearance.cancelButtonColor;

_useCoreGraphics = appearance.useCoreGraphics;
}

return self;
}

Expand Down Expand Up @@ -809,22 +815,61 @@ - (void)setupButtons
{
self.buttons = [[NSMutableArray alloc] initWithCapacity:self.items.count];
for (NSUInteger i = 0; i < self.items.count; i++) {
UIButton *button = [self buttonForItemIndex:i];
UIButton *button;
if (self.useCoreGraphics) {
button = [self buttonForCoreGraphicsItemIndex:i];
}
else {
button = [self buttonForItemIndex:i];
}
[self.buttons addObject:button];
[self.containerView addSubview:button];
}
}

- (UIButton *)buttonForCoreGraphicsItemIndex:(NSUInteger)index
{
SIAlertItem *item = self.items[index];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.titleLabel.font = self.buttonFont;

// Set button colors
switch (item.type) {
case SIAlertViewButtonTypeCancel:
item.buttonColor = self.cancelButtonColor;
[button setTitleColor:self.cancelButtonFontColor forState:UIControlStateNormal];
[button setTitleColor:self.cancelButtonFontColor forState:UIControlStateHighlighted];
break;
case SIAlertViewButtonTypeDestructive:
item.buttonColor = self.destructiveButtonColor;
[button setTitleColor:self.destructiveButtonFontColor forState:UIControlStateNormal];
[button setTitleColor:self.destructiveButtonFontColor forState:UIControlStateHighlighted];
break;
case SIAlertViewButtonTypeDefault:
default:
item.buttonColor = self.defaultButtonColor;
[button setTitleColor:self.defaultButtonFontColor forState:UIControlStateNormal];
[button setTitleColor:self.defaultButtonFontColor forState:UIControlStateHighlighted];
break;
}

UIImage *normalImage = [item imageForButton];
UIImage *highlightedImage = [item imageForButtonHighlighted];

return [self finishCreatingButton:button
forItem:item
withNormalImage:normalImage
andHighlightedImage:highlightedImage];
}

- (UIButton *)buttonForItemIndex:(NSUInteger)index
{
SIAlertItem *item = self.items[index];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.tag = index;
button.autoresizingMask = UIViewAutoresizingFlexibleWidth;
button.titleLabel.font = self.buttonFont;
[button setTitle:item.title forState:UIControlStateNormal];
UIImage *normalImage = nil;
UIImage *highlightedImage = nil;

switch (item.type) {
case SIAlertViewButtonTypeCancel:
normalImage = [UIImage imageNamed:@"SIAlertView.bundle/button-cancel"];
Expand All @@ -846,6 +891,22 @@ - (UIButton *)buttonForItemIndex:(NSUInteger)index
[button setTitleColor:[UIColor colorWithWhite:0.4 alpha:0.8] forState:UIControlStateHighlighted];
break;
}

return [self finishCreatingButton:button
forItem:item
withNormalImage:normalImage
andHighlightedImage:highlightedImage];
}

- (UIButton *)finishCreatingButton:(UIButton *)button
forItem:(SIAlertItem *)item
withNormalImage:(UIImage *)normalImage
andHighlightedImage:(UIImage *)highlightedImage
{
button.tag = [self.items indexOfObject:item];
button.autoresizingMask = UIViewAutoresizingFlexibleWidth;
button.titleLabel.font = self.buttonFont;
[button setTitle:item.title forState:UIControlStateNormal];
CGFloat hInset = floorf(normalImage.size.width / 2);
CGFloat vInset = floorf(normalImage.size.height / 2);
UIEdgeInsets insets = UIEdgeInsetsMake(vInset, hInset, vInset, hInset);
Expand Down Expand Up @@ -920,6 +981,55 @@ - (void)setMessageColor:(UIColor *)messageColor
self.messageLabel.textColor = messageColor;
}

- (void)setDefaultButtonFontColor:(UIColor *)defaultButtonFontColor
{
if (_defaultButtonFontColor == defaultButtonFontColor) {
return;
}
_defaultButtonFontColor = defaultButtonFontColor;
}

- (void)setDefaultButtonColor:(UIColor *)defaultButtonColor
{
if (_defaultButtonColor == defaultButtonColor) {
return;
}

_defaultButtonColor = defaultButtonColor;
}

-(void)setDestructiveButtonFontColor:(UIColor *)destructiveButtonFontColor
{
if (_destructiveButtonFontColor == destructiveButtonFontColor) {
return;
}
_destructiveButtonFontColor = destructiveButtonFontColor;
}

- (void)setDestructiveButtonColor:(UIColor *)destructiveButtonColor
{
if (_destructiveButtonColor == destructiveButtonColor) {
return;
}
_destructiveButtonColor = destructiveButtonColor;
}

- (void)setCancelButtonFontColor:(UIColor *)cancelButtonFontColor
{
if (_cancelButtonFontColor == cancelButtonFontColor) {
return;
}
_cancelButtonFontColor = cancelButtonFontColor;
}

- (void)setCancelButtonColor:(UIColor *)cancelButtonColor
{
if (_cancelButtonColor == cancelButtonColor) {
return;
}
_cancelButtonColor = cancelButtonColor;
}

- (void)setButtonFont:(UIFont *)buttonFont
{
if (_buttonFont == buttonFont) {
Expand Down
Loading