Skip to content
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
24 changes: 13 additions & 11 deletions ALAlertBanner/ALAlertBanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

typedef enum {
ALAlertBannerStyleSuccess = 0,
ALAlertBannerStyleFailure,
ALAlertBannerStyleNotify,
ALAlertBannerStyleWarning,
} ALAlertBannerStyle;

typedef enum {
ALAlertBannerPositionTop = 0,
ALAlertBannerPositionBottom,
Expand All @@ -47,9 +40,11 @@ typedef enum {
ALAlertBannerStateHidden
} ALAlertBannerState;

@protocol ALBannerStyleConfigurationProtocol;

@interface ALAlertBanner : UIView

@property (nonatomic, readonly) ALAlertBannerStyle style;
@property (nonatomic, readonly) id<ALBannerStyleConfigurationProtocol> styleConfiguration;
@property (nonatomic, readonly) ALAlertBannerPosition position;
@property (nonatomic, readonly) ALAlertBannerState state;

Expand Down Expand Up @@ -88,19 +83,26 @@ typedef enum {
*/
@property (nonatomic) CGFloat bannerOpacity;

/**
Dissable default initializer.

Use [ALAlertBanner alertBannerForView:styleConfiguration:position:title] and etc.
*/
- (instancetype)init NS_UNAVAILABLE;

/**
The default methods to customize and display a banner.
*/
+ (ALAlertBanner *)alertBannerForView:(UIView *)view style:(ALAlertBannerStyle)style position:(ALAlertBannerPosition)position title:(NSString *)title;
+ (ALAlertBanner *)alertBannerForView:(UIView *)view styleConfiguration:(id<ALBannerStyleConfigurationProtocol>)styleConfiguration position:(ALAlertBannerPosition)position title:(NSString *)title;

+ (ALAlertBanner *)alertBannerForView:(UIView *)view style:(ALAlertBannerStyle)style position:(ALAlertBannerPosition)position title:(NSString *)title subtitle:(NSString *)subtitle;
+ (ALAlertBanner *)alertBannerForView:(UIView *)view styleConfiguration:(id<ALBannerStyleConfigurationProtocol>)styleConfiguration position:(ALAlertBannerPosition)position title:(NSString *)title subtitle:(NSString *)subtitle;

/**
Optional method to handle a tap on a banner.

By default, supplying a tap handler will disable allowTapToDismiss on this particular banner. If you want to reinstate this behavior alongside the tap handler, you can call `[[ALAlertBannerManager sharedManager] hideAlertBanner:alertBanner];` in tappedBlock().
*/
+ (ALAlertBanner *)alertBannerForView:(UIView *)view style:(ALAlertBannerStyle)style position:(ALAlertBannerPosition)position title:(NSString *)title subtitle:(NSString *)subtitle tappedBlock:(void(^)(ALAlertBanner *alertBanner))tappedBlock;
+ (ALAlertBanner *)alertBannerForView:(UIView *)view styleConfiguration:(id<ALBannerStyleConfigurationProtocol>)styleConfiguration position:(ALAlertBannerPosition)position title:(NSString *)title subtitle:(NSString *)subtitle tappedBlock:(void(^)(ALAlertBanner *alertBanner))tappedBlock;

/**
Show the alert banner
Expand Down
96 changes: 23 additions & 73 deletions ALAlertBanner/ALAlertBanner.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ this software and associated documentation files (the "Software"), to deal in
#import <QuartzCore/QuartzCore.h>
#import "ALAlertBanner+Private.h"
#import "ALAlertBannerManager.h"
#import "ALBannerStyleConfiguration.h"

static NSString * const kShowAlertBannerKey = @"showAlertBannerKey";
static NSString * const kHideAlertBannerKey = @"hideAlertBannerKey";
Expand Down Expand Up @@ -114,7 +115,6 @@ @interface ALAlertBanner () {
ALAlertBannerManager *manager;
}

@property (nonatomic, assign) ALAlertBannerStyle style;
@property (nonatomic, assign) ALAlertBannerPosition position;
@property (nonatomic, assign) ALAlertBannerState state;
@property (nonatomic) NSTimeInterval fadeOutDuration;
Expand All @@ -123,15 +123,16 @@ @interface ALAlertBanner () {
@property (nonatomic, strong) UILabel *subtitleLabel;
@property (nonatomic, strong) UIImageView *styleImageView;
@property (nonatomic) CGRect parentFrameUponCreation;
@property (nonatomic, readwrite) id<ALBannerStyleConfigurationProtocol> styleConfiguration;

@end

@implementation ALAlertBanner

- (id)init {
- (instancetype)init:(id<ALBannerStyleConfigurationProtocol>)styleConfiguration {
self = [super init];
if (self) {

self.styleConfiguration = styleConfiguration;
[self commonInit];

}
Expand Down Expand Up @@ -172,58 +173,37 @@ - (void)setupSubviews {
_titleLabel = [[UILabel alloc] init];
_titleLabel.backgroundColor = [UIColor clearColor];
_titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:13.f];
_titleLabel.textColor = [UIColor colorWithWhite:1.f alpha:0.9f];
_titleLabel.textColor = self.styleConfiguration.titleTextColor;
_titleLabel.textAlignment = NSTextAlignmentLeft;
_titleLabel.numberOfLines = 1;
_titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
_titleLabel.layer.shadowColor = [UIColor blackColor].CGColor;
_titleLabel.layer.shadowOffset = CGSizeMake(0.f, -1.f);
_titleLabel.layer.shadowOpacity = 0.3f;
_titleLabel.layer.shadowOpacity = self.styleConfiguration.labelsShadowOpacity;
_titleLabel.layer.shadowRadius = 0.f;
[self addSubview:_titleLabel];

_subtitleLabel = [[UILabel alloc] init];
_subtitleLabel.backgroundColor = [UIColor clearColor];
_subtitleLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:10.f];
_subtitleLabel.textColor = [UIColor colorWithWhite:1.f alpha:0.9f];
_subtitleLabel.textColor = self.styleConfiguration.subtitleTextColor;
_subtitleLabel.textAlignment = NSTextAlignmentLeft;
_subtitleLabel.numberOfLines = 0;
_subtitleLabel.lineBreakMode = NSLineBreakByWordWrapping;
_subtitleLabel.layer.shadowColor = [UIColor blackColor].CGColor;
_subtitleLabel.layer.shadowOffset = CGSizeMake(0.f, -1.f);
_subtitleLabel.layer.shadowOpacity = 0.3f;
_subtitleLabel.layer.shadowOpacity = self.styleConfiguration.labelsShadowOpacity;
_subtitleLabel.layer.shadowRadius = 0.f;
[self addSubview:_subtitleLabel];
}

# pragma mark -
# pragma mark Custom Setters & Getters

-(void)setStyle:(ALAlertBannerStyle)style {
_style = style;
-(void)setStyleConfiguration:(id<ALBannerStyleConfigurationProtocol>)styleConfiguration {
_styleConfiguration = styleConfiguration;

switch (style) {
case ALAlertBannerStyleSuccess:
self.styleImageView.image = [UIImage imageNamed:@"bannerSuccess.png"];
break;

case ALAlertBannerStyleFailure:
self.styleImageView.image = [UIImage imageNamed:@"bannerFailure.png"];
break;

case ALAlertBannerStyleNotify:
self.styleImageView.image = [UIImage imageNamed:@"bannerNotify.png"];
break;

case ALAlertBannerStyleWarning:
self.styleImageView.image = [UIImage imageNamed:@"bannerAlert.png"];

//tone the shadows down a little for the yellow background
self.titleLabel.layer.shadowOpacity = 0.2f;
self.subtitleLabel.layer.shadowOpacity = 0.2f;

break;
}
self.styleImageView.image = self.styleConfiguration.image;
}

- (void)setShowShadow:(BOOL)showShadow {
Expand All @@ -240,9 +220,7 @@ - (void)setShowShadow:(BOOL)showShadow {
self.layer.shadowPath = [UIBezierPath bezierPathWithRect:shadowPath].CGPath;

self.fadeInDuration = 0.15f;
}

else {
} else {
newShadowRadius = 0.f;
self.layer.shadowRadius = 0.f;
self.layer.shadowOffset = CGSizeZero;
Expand Down Expand Up @@ -296,16 +274,16 @@ + (void)forceHideAllAlertBannersInView:(UIView *)view {
[[ALAlertBannerManager sharedManager] forceHideAllAlertBannersInView:view];
}

+ (ALAlertBanner *)alertBannerForView:(UIView *)view style:(ALAlertBannerStyle)style position:(ALAlertBannerPosition)position title:(NSString *)title {
return [self alertBannerForView:view style:style position:position title:title subtitle:nil tappedBlock:nil];
+ (ALAlertBanner *)alertBannerForView:(UIView *)view styleConfiguration:(id<ALBannerStyleConfigurationProtocol>)styleConfiguration position:(ALAlertBannerPosition)position title:(NSString *)title {
return [self alertBannerForView:view styleConfiguration:styleConfiguration position:position title:title subtitle:nil tappedBlock:nil];
}

+ (ALAlertBanner *)alertBannerForView:(UIView *)view style:(ALAlertBannerStyle)style position:(ALAlertBannerPosition)position title:(NSString *)title subtitle:(NSString *)subtitle {
return [self alertBannerForView:view style:style position:position title:title subtitle:subtitle tappedBlock:nil];
+ (ALAlertBanner *)alertBannerForView:(UIView *)view styleConfiguration:(id<ALBannerStyleConfigurationProtocol>)styleConfiguration position:(ALAlertBannerPosition)position title:(NSString *)title subtitle:(NSString *)subtitle {
return [self alertBannerForView:view styleConfiguration:styleConfiguration position:position title:title subtitle:subtitle tappedBlock:nil];
}

+ (ALAlertBanner *)alertBannerForView:(UIView *)view style:(ALAlertBannerStyle)style position:(ALAlertBannerPosition)position title:(NSString *)title subtitle:(NSString *)subtitle tappedBlock:(void (^)(ALAlertBanner *alertBanner))tappedBlock {
ALAlertBanner *alertBanner = [ALAlertBanner createAlertBannerForView:view style:style position:position title:title subtitle:subtitle];
+ (ALAlertBanner *)alertBannerForView:(UIView *)view styleConfiguration:(id<ALBannerStyleConfigurationProtocol>)styleConfiguration position:(ALAlertBannerPosition)position title:(NSString *)title subtitle:(NSString *)subtitle tappedBlock:(void (^)(ALAlertBanner *alertBanner))tappedBlock {
ALAlertBanner *alertBanner = [ALAlertBanner createAlertBannerForView:view styleConfiguration:styleConfiguration position:position title:title subtitle:subtitle];
alertBanner.allowTapToDismiss = tappedBlock ? NO : alertBanner.allowTapToDismiss;
alertBanner.tappedBlock = tappedBlock;
return alertBanner;
Expand All @@ -314,15 +292,14 @@ + (ALAlertBanner *)alertBannerForView:(UIView *)view style:(ALAlertBannerStyle)s
# pragma mark -
# pragma mark Internal Class Methods

+ (ALAlertBanner *)createAlertBannerForView:(UIView *)view style:(ALAlertBannerStyle)style position:(ALAlertBannerPosition)position title:(NSString *)title subtitle:(NSString *)subtitle {
ALAlertBanner *alertBanner = [[ALAlertBanner alloc] init];
+ (ALAlertBanner *)createAlertBannerForView:(UIView *)view styleConfiguration:(id<ALBannerStyleConfigurationProtocol>)styleConfiguration position:(ALAlertBannerPosition)position title:(NSString *)title subtitle:(NSString *)subtitle {
ALAlertBanner *alertBanner = [[ALAlertBanner alloc] init:styleConfiguration];

if (![view isKindOfClass:[UIWindow class]] && position == ALAlertBannerPositionUnderNavBar)
[[NSException exceptionWithName:@"Wrong ALAlertBannerPosition For View Type" reason:@"ALAlertBannerPositionUnderNavBar should only be used if you are presenting the alert banner on the AppDelegate window. Use ALAlertBannerPositionTop or ALAlertBannerPositionBottom for normal UIViews" userInfo:nil] raise];

alertBanner.titleLabel.text = !title ? @" " : title;
alertBanner.subtitleLabel.text = subtitle;
alertBanner.style = style;
alertBanner.position = position;
alertBanner.state = ALAlertBannerStateHidden;

Expand Down Expand Up @@ -696,21 +673,8 @@ - (void)updatePositionAfterRotationWithY:(CGFloat)yPos animated:(BOOL)animated {
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();

UIColor *fillColor;
switch (self.style) {
case ALAlertBannerStyleSuccess:
fillColor = [UIColor colorWithRed:(77/255.0) green:(175/255.0) blue:(67/255.0) alpha:1.f];
break;
case ALAlertBannerStyleFailure:
fillColor = [UIColor colorWithRed:(173/255.0) green:(48/255.0) blue:(48/255.0) alpha:1.f];
break;
case ALAlertBannerStyleNotify:
fillColor = [UIColor colorWithRed:(48/255.0) green:(110/255.0) blue:(173/255.0) alpha:1.f];
break;
case ALAlertBannerStyleWarning:
fillColor = [UIColor colorWithRed:(211/255.0) green:(209/255.0) blue:(100/255.0) alpha:1.f];
break;
}
UIColor *fillColor;
fillColor = self.styleConfiguration.fillColor;

NSArray *colorsArray = [NSArray arrayWithObjects:(id)[fillColor CGColor], (id)[[fillColor darkerColor] CGColor], nil];
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
Expand Down Expand Up @@ -740,21 +704,7 @@ - (id)nextAvailableViewController:(id)view {
}

- (NSString *)description {
NSString *styleString;
switch (self.style) {
case ALAlertBannerStyleSuccess:
styleString = @"ALAlertBannerStyleSuccess";
break;
case ALAlertBannerStyleFailure:
styleString = @"ALAlertBannerStyleFailure";
break;
case ALAlertBannerStyleNotify:
styleString = @"ALAlertBannerStyleNotify";
break;
case ALAlertBannerStyleWarning:
styleString = @"ALAlertBannerStyleWarning";
break;
}
NSString *styleString = self.styleConfiguration.styleString;
NSString *positionString;
switch (self.position) {
case ALAlertBannerPositionTop:
Expand Down
18 changes: 18 additions & 0 deletions ALAlertBanner/ALBannerStyleConfiguration.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

#import <Foundation/Foundation.h>

@protocol ALBannerStyleConfigurationProtocol <NSObject>
@property (nonatomic, readonly) UIColor *fillColor;
@property (nonatomic, readonly) UIColor *titleTextColor;
@property (nonatomic, readonly) UIColor *subtitleTextColor;
@property (nonatomic, readonly) UIImage *image;
@property (nonatomic, readonly) CGFloat labelsShadowOpacity;
@property (nonatomic, readonly) NSString *styleString;
@end

@interface ALBannerStyleConfiguration : NSObject <ALBannerStyleConfigurationProtocol>
+ (ALBannerStyleConfiguration *)failureStyleConfiguration;
+ (ALBannerStyleConfiguration *)successStyleConfiguration;
+ (ALBannerStyleConfiguration *)notifyStyleConfiguration;
+ (ALBannerStyleConfiguration *)warningStyleConfiguration;
@end
75 changes: 75 additions & 0 deletions ALAlertBanner/ALBannerStyleConfiguration.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@

#import "ALBannerStyleConfiguration.h"

@interface ALBannerStyleConfiguration ()
@property (nonatomic, strong) UIColor *fillColor;
@property (nonatomic, strong) UIColor *titleTextColor;
@property (nonatomic, strong) UIColor *subtitleTextColor;
@property (nonatomic, strong) UIImage *image;
@property (nonatomic, assign) CGFloat labelsShadowOpacity;
@property (nonatomic, strong) NSString *styleString;
@end

@implementation ALBannerStyleConfiguration

+ (ALBannerStyleConfiguration *)failureStyleConfiguration {
static ALBannerStyleConfiguration *failureConfiguration;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
failureConfiguration = [[ALBannerStyleConfiguration alloc] init];
failureConfiguration.fillColor = [UIColor colorWithRed:(173/255.0) green:(48/255.0) blue:(48/255.0) alpha:1.f];
failureConfiguration.titleTextColor = [UIColor colorWithWhite:1.f alpha:0.9f];
failureConfiguration.subtitleTextColor = [UIColor colorWithWhite:1.f alpha:0.9f];
failureConfiguration.image = [UIImage imageNamed:@"bannerFailure.png"];
failureConfiguration.labelsShadowOpacity = 0.3;
failureConfiguration.styleString = @"ALAlertBannerStyleFailure";
});
return failureConfiguration;
}

+ (ALBannerStyleConfiguration *)successStyleConfiguration {
static ALBannerStyleConfiguration *successConfiguration;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
successConfiguration = [[ALBannerStyleConfiguration alloc] init];
successConfiguration.fillColor = [UIColor colorWithRed:(77/255.0) green:(175/255.0) blue:(67/255.0) alpha:1.f];
successConfiguration.titleTextColor = [UIColor colorWithWhite:1.f alpha:0.9f];
successConfiguration.subtitleTextColor = [UIColor colorWithWhite:1.f alpha:0.9f];
successConfiguration.image = [UIImage imageNamed:@"bannerSuccess.png"];
successConfiguration.labelsShadowOpacity = 0.3;
successConfiguration.styleString = @"ALAlertBannerStyleSuccess";
});
return successConfiguration;
}

+ (ALBannerStyleConfiguration *)notifyStyleConfiguration {
static ALBannerStyleConfiguration *notifyConfiguration;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
notifyConfiguration = [[ALBannerStyleConfiguration alloc] init];
notifyConfiguration.fillColor = [UIColor colorWithRed:(48/255.0) green:(110/255.0) blue:(173/255.0) alpha:1.f];
notifyConfiguration.titleTextColor = [UIColor colorWithWhite:1.f alpha:0.9f];
notifyConfiguration.subtitleTextColor = [UIColor colorWithWhite:1.f alpha:0.9f];
notifyConfiguration.image = [UIImage imageNamed:@"bannerNotify.png"];
notifyConfiguration.labelsShadowOpacity = 0.3;
notifyConfiguration.styleString = @"ALAlertBannerStyleNotify";
});
return notifyConfiguration;
}

+ (ALBannerStyleConfiguration *)warningStyleConfiguration {
static ALBannerStyleConfiguration *warningConfiguration;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
warningConfiguration = [[ALBannerStyleConfiguration alloc] init];
warningConfiguration.fillColor = [UIColor colorWithRed:(211/255.0) green:(209/255.0) blue:(100/255.0) alpha:1.f];
warningConfiguration.titleTextColor = [UIColor colorWithWhite:1.f alpha:0.9f];
warningConfiguration.subtitleTextColor = [UIColor colorWithWhite:1.f alpha:0.9f];
warningConfiguration.image = [UIImage imageNamed:@"bannerAlert.png"];
warningConfiguration.labelsShadowOpacity = 0.2;
warningConfiguration.styleString = @"ALAlertBannerStyleWarning";
});
return warningConfiguration;
}

@end
6 changes: 6 additions & 0 deletions ALAlertBannerDemo/ALAlertBannerDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
4FC799DF17BC33D0000AEC49 /* bannerNotify.png in Resources */ = {isa = PBXBuildFile; fileRef = 4FC799DB17BC33D0000AEC49 /* bannerNotify.png */; };
4FC799E017BC33D0000AEC49 /* bannerSuccess.png in Resources */ = {isa = PBXBuildFile; fileRef = 4FC799DC17BC33D0000AEC49 /* bannerSuccess.png */; };
4FFBA6F917BE970700C2A7F2 /* ALAlertBannerManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 4FFBA6F817BE970700C2A7F2 /* ALAlertBannerManager.m */; };
D654E67B2077742100B8B286 /* ALBannerStyleConfiguration.m in Sources */ = {isa = PBXBuildFile; fileRef = D654E67A2077742100B8B286 /* ALBannerStyleConfiguration.m */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -63,6 +64,8 @@
4FC799DC17BC33D0000AEC49 /* bannerSuccess.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = bannerSuccess.png; sourceTree = "<group>"; };
4FFBA6F717BE970700C2A7F2 /* ALAlertBannerManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ALAlertBannerManager.h; sourceTree = "<group>"; };
4FFBA6F817BE970700C2A7F2 /* ALAlertBannerManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ALAlertBannerManager.m; sourceTree = "<group>"; };
D654E6792077742100B8B286 /* ALBannerStyleConfiguration.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ALBannerStyleConfiguration.h; sourceTree = "<group>"; };
D654E67A2077742100B8B286 /* ALBannerStyleConfiguration.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ALBannerStyleConfiguration.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -140,6 +143,8 @@
4FC799C617BC078A000AEC49 /* ALAlertBanner */ = {
isa = PBXGroup;
children = (
D654E6792077742100B8B286 /* ALBannerStyleConfiguration.h */,
D654E67A2077742100B8B286 /* ALBannerStyleConfiguration.m */,
4FC799CA17BC078B000AEC49 /* ALAlertBanner.h */,
4FC799CB17BC078B000AEC49 /* ALAlertBanner.m */,
4FFBA6F717BE970700C2A7F2 /* ALAlertBannerManager.h */,
Expand Down Expand Up @@ -243,6 +248,7 @@
4FC799B317BC072A000AEC49 /* main.m in Sources */,
4FC799B717BC072A000AEC49 /* AppDelegate.m in Sources */,
4FC799C517BC0758000AEC49 /* ViewController.m in Sources */,
D654E67B2077742100B8B286 /* ALBannerStyleConfiguration.m in Sources */,
4FC799CD17BC078B000AEC49 /* ALAlertBanner.m in Sources */,
4FFBA6F917BE970700C2A7F2 /* ALAlertBannerManager.m in Sources */,
);
Expand Down
Loading