diff --git a/BlockAlertsAnd-ActionSheets.podspec b/BlockAlertsAnd-ActionSheets.podspec index 557a079..30c3499 100644 --- a/BlockAlertsAnd-ActionSheets.podspec +++ b/BlockAlertsAnd-ActionSheets.podspec @@ -1,15 +1,15 @@ Pod::Spec.new do |s| s.name = "BlockAlertsAnd-ActionSheets" - s.version = "1.0.6" + s.version = "1.0.11" s.summary = 'Beautifully done UIAlertView and UIActionSheet replacements inspired by TweetBot.' - s.homepage = "https://github.com/gpambrozio/BlockAlertsAnd-ActionSheets" + s.homepage = "https://github.com/pventura1976/BlockAlertsAnd-ActionSheets" s.license = 'MIT' - s.author = { 'Gustavo Ambrozio' => '', "Barrett Jacobsen" => "admin@barrettj.com", "Jose Santiago Jr" => '' } - s.source = { :git => 'https://github.com/gpambrozio/BlockAlertsAnd-ActionSheets.git', :tag => "#{s.version}" } + s.author = { 'Gustavo Ambrozio' => '', "Barrett Jacobsen" => "admin@barrettj.com", "Jose Santiago Jr" => '', 'Taymer Ragazzini' => 'tragazzini@gmail.com', 'Pedro Ventura' => '' } + s.source = { :git => 'https://github.com/pventura1976/BlockAlertsAnd-ActionSheets.git', :tag => "#{s.version}" } s.platform = :ios, '4.3' - s.source_files = "BlockAlertsDemo/ToAddToYourProjects/BlockActionSheet.{h,m}", "BlockAlertsDemo/ToAddToYourProjects/BlockAlertView.{h,m}", "BlockAlertsDemo/ToAddToYourProjects/BlockBackground.{h,m}", "BlockAlertsDemo/ToAddToYourProjects/BlockTextPromptAlertView.{h,m}", 'BlockAlertsDemo/ProjectSpecific/BlockUI.h' + s.source_files = "BlockAlertsDemo/ToAddToYourProjects/BlockActionSheet.{h,m}", "BlockAlertsDemo/ToAddToYourProjects/BlockAlertView.{h,m}", "BlockAlertsDemo/ToAddToYourProjects/BlockBackground.{h,m}", "BlockAlertsDemo/ToAddToYourProjects/BlockTextPromptAlertView.{h,m}", "BlockAlertsDemo/ProjectSpecific/BlockUI.h" s.resources = "BlockAlertsDemo/images/button*.png", "BlockAlertsDemo/images/ActionSheet/*.png", "BlockAlertsDemo/images/AlertView/*.png" - + s.requires_arc = false s.subspec 'TableAlertView' do |table| table.source_files = "BlockAlertsDemo/ToAddToYourProjects/BlockTableAlertView.{h,m}" end diff --git a/BlockAlertsDemo.xcodeproj/project.pbxproj b/BlockAlertsDemo.xcodeproj/project.pbxproj index 404281e..fd519f8 100644 --- a/BlockAlertsDemo.xcodeproj/project.pbxproj +++ b/BlockAlertsDemo.xcodeproj/project.pbxproj @@ -296,7 +296,11 @@ EC90169114BB629F00EF52E1 /* Project object */ = { isa = PBXProject; attributes = { +<<<<<<< HEAD + LastUpgradeCheck = 0610; +======= LastUpgradeCheck = 0630; +>>>>>>> gpambrozio/master ORGANIZATIONNAME = "CodeCrop Software"; }; buildConfigurationList = EC90169414BB629F00EF52E1 /* Build configuration list for PBXProject "BlockAlertsDemo" */; diff --git a/BlockAlertsDemo/ToAddToYourProjects/BlockActionSheet.h b/BlockAlertsDemo/ToAddToYourProjects/BlockActionSheet.h index ef160ce..d8284d5 100755 --- a/BlockAlertsDemo/ToAddToYourProjects/BlockActionSheet.h +++ b/BlockAlertsDemo/ToAddToYourProjects/BlockActionSheet.h @@ -12,24 +12,38 @@ @private UIView *_view; NSMutableArray *_blocks; + NSMutableArray *_completionBlocks; CGFloat _height; + UIColor *_tintColor; } -@property (nonatomic, readonly) UIView *view; -@property (nonatomic, readwrite) BOOL vignetteBackground; +@property(nonatomic, readonly) UIView *view; +@property(nonatomic, readwrite) BOOL vignetteBackground; + (id)sheetWithTitle:(NSString *)title; ++ (id)sheetWithTitle:(NSString *)title tintColor:(UIColor *)tintColor textColor:(UIColor *)textColor; +- (id)initWithTitle:(NSString *)title tintColor:(UIColor *)tintColor textColor:(UIColor *)textColor; -- (id)initWithTitle:(NSString *)title; - -- (void)setCancelButtonWithTitle:(NSString *) title block:(void (^)()) block; -- (void)setDestructiveButtonWithTitle:(NSString *) title block:(void (^)()) block; -- (void)addButtonWithTitle:(NSString *) title block:(void (^)()) block; +- (void)setCancelButtonWithTitle:(NSString *)title block:(void (^)())block; +- (void)setDestructiveButtonWithTitle:(NSString *)title block:(void (^)())block; +- (void)addButtonWithTitle:(NSString *)title block:(void (^)())block; +- (void)addButtonWithTitle:(NSString *)title color:(NSString *)color block:(void (^)())block atIndex:(NSInteger)index; +// Add buttons at index with block - (void)setCancelButtonWithTitle:(NSString *) title atIndex:(NSInteger)index block:(void (^)()) block; - (void)setDestructiveButtonWithTitle:(NSString *) title atIndex:(NSInteger)index block:(void (^)()) block; - (void)addButtonWithTitle:(NSString *) title atIndex:(NSInteger)index block:(void (^)()) block; +// Add button with block and animation completion block +- (void)setCancelButtonWithTitle:(NSString *)title block:(void (^)())block completion:(void (^)())completionBlock; +- (void)setDestructiveButtonWithTitle:(NSString *)title block:(void (^)())block completion:(void (^)())completionBlock; +- (void)addButtonWithTitle:(NSString *)title block:(void (^)())block completion:(void (^)())completionBlock; + +// Add button at index with block and animation completion block +- (void)setCancelButtonWithTitle:(NSString *)title atIndex:(NSInteger)index block:(void (^)())block completion:(void (^)())completionBlock; +- (void)setDestructiveButtonWithTitle:(NSString *)title atIndex:(NSInteger)index block:(void (^)())block completion:(void (^)())completionBlock; +- (void)addButtonWithTitle:(NSString *)title atIndex:(NSInteger)index block:(void (^)())block completion:(void (^)())completionBlock; + - (void)showInView:(UIView *)view; - (NSUInteger)buttonCount; diff --git a/BlockAlertsDemo/ToAddToYourProjects/BlockActionSheet.m b/BlockAlertsDemo/ToAddToYourProjects/BlockActionSheet.m index 17dcd65..fc1bc16 100755 --- a/BlockAlertsDemo/ToAddToYourProjects/BlockActionSheet.m +++ b/BlockAlertsDemo/ToAddToYourProjects/BlockActionSheet.m @@ -18,10 +18,8 @@ @implementation BlockActionSheet #pragma mark - init -+ (void)initialize -{ - if (self == [BlockActionSheet class]) - { ++ (void)initialize { + if (self == [BlockActionSheet class]) { background = [UIImage imageNamed:kActionSheetBackground]; background = [[background stretchableImageWithLeftCapWidth:0 topCapHeight:kActionSheetBackgroundCapHeight] retain]; titleFont = [kActionSheetTitleFont retain]; @@ -29,15 +27,16 @@ + (void)initialize } } -+ (id)sheetWithTitle:(NSString *)title -{ - return [[[BlockActionSheet alloc] initWithTitle:title] autorelease]; ++ (id)sheetWithTitle:(NSString *)title { + return [[[BlockActionSheet alloc] initWithTitle:title tintColor:nil textColor:nil] autorelease]; } -- (id)initWithTitle:(NSString *)title -{ - if ((self = [super init])) - { ++ (id)sheetWithTitle:(NSString *)title tintColor:(UIColor *)tintColor textColor:(UIColor *)textColor { + return [[[BlockActionSheet alloc] initWithTitle:title tintColor:tintColor textColor:textColor] autorelease]; +} + +- (id)initWithTitle:(NSString *)title tintColor:(UIColor *)tintColor textColor:(UIColor *)textColor { + if ((self = [super init])) { UIWindow *parentView = [BlockBackground sharedInstance]; CGRect frame = parentView.bounds; @@ -47,27 +46,21 @@ - (id)initWithTitle:(NSString *)title _blocks = [[NSMutableArray alloc] init]; _height = kActionSheetTopMargin; - - if (title) - { - CGSize size; - if (IOS_LESS_THAN_7) { -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdeprecated-declarations" - size = [title sizeWithFont:titleFont constrainedToSize:CGSizeMake(frame.size.width-kActionSheetBorder*2, 1000) lineBreakMode:NSLineBreakByWordWrapping]; -#pragma clang diagnostic pop - } - else { - NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; - paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping; - size = [title boundingRectWithSize:CGSizeMake(frame.size.width-kAlertViewBorder*2, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSParagraphStyleAttributeName : paragraphStyle, NSFontAttributeName : titleFont} context:nil].size; - size = CGSizeMake(ceilf(size.width), ceilf(size.height)); - } + _tintColor = [tintColor retain]; + if (title) { + CGSize size = [title sizeWithFont:titleFont + constrainedToSize:CGSizeMake(frame.size.width - kActionSheetBorder * 2, 1000) + lineBreakMode:NSLineBreakByWordWrapping]; - UILabel *labelView = [[UILabel alloc] initWithFrame:CGRectMake(kActionSheetBorder, _height, frame.size.width-kActionSheetBorder*2, size.height)]; + UILabel *labelView = + [[UILabel alloc] initWithFrame:CGRectMake(kActionSheetBorder, _height, + frame.size.width - kActionSheetBorder * 2, size.height)]; labelView.font = titleFont; labelView.numberOfLines = 0; labelView.lineBreakMode = NSLineBreakByWordWrapping; + if (textColor) + labelView.textColor = textColor; + else labelView.textColor = kActionSheetTitleTextColor; labelView.backgroundColor = [UIColor clearColor]; labelView.textAlignment = NSTextAlignmentCenter; @@ -76,7 +69,7 @@ - (id)initWithTitle:(NSString *)title labelView.text = title; labelView.autoresizingMask = UIViewAutoresizingFlexibleWidth; - + [_view addSubview:labelView]; [labelView release]; @@ -88,19 +81,31 @@ - (id)initWithTitle:(NSString *)title return self; } -- (void) dealloc +- (void) dealloc { [_view release]; [_blocks release]; + [_completionBlocks release]; + [_tintColor release]; [super dealloc]; } -- (NSUInteger)buttonCount -{ +- (NSUInteger)buttonCount { return _blocks.count; } +#pragma mark - Add buttons + - (void)addButtonWithTitle:(NSString *)title color:(NSString*)color block:(void (^)())block atIndex:(NSInteger)index +{ + [self addButtonWithTitle:title + color:color + block:block + atIndex:index + completion:nil]; +} + +- (void)addButtonWithTitle:(NSString *)title color:(NSString*)color block:(void (^)())block atIndex:(NSInteger)index completion:(void (^)())completionBlock { if (index >= 0) { @@ -110,6 +115,12 @@ - (void)addButtonWithTitle:(NSString *)title color:(NSString*)color block:(void color, nil] atIndex:index]; + [_blocks insertObject:[NSArray arrayWithObjects: + completionBlock ? [[completionBlock copy] autorelease] : [NSNull null], + title, + color, + nil] + atIndex:index]; } else { @@ -118,39 +129,74 @@ - (void)addButtonWithTitle:(NSString *)title color:(NSString*)color block:(void title, color, nil]]; + [_completionBlocks addObject:[NSArray arrayWithObjects: + completionBlock ? [[completionBlock copy] autorelease] : [NSNull null], + title, + color, + nil]]; } } -- (void)setDestructiveButtonWithTitle:(NSString *)title block:(void (^)())block -{ +- (void)setDestructiveButtonWithTitle:(NSString *)title block:(void (^)())block { [self addButtonWithTitle:title color:@"red" block:block atIndex:-1]; } -- (void)setCancelButtonWithTitle:(NSString *)title block:(void (^)())block -{ +- (void)setCancelButtonWithTitle:(NSString *)title block:(void (^)())block { [self addButtonWithTitle:title color:@"black" block:block atIndex:-1]; } -- (void)addButtonWithTitle:(NSString *)title block:(void (^)())block -{ +- (void)addButtonWithTitle:(NSString *)title block:(void (^)())block { [self addButtonWithTitle:title color:@"gray" block:block atIndex:-1]; } -- (void)setDestructiveButtonWithTitle:(NSString *)title atIndex:(NSInteger)index block:(void (^)())block -{ +- (void)setDestructiveButtonWithTitle:(NSString *)title atIndex:(NSInteger)index block:(void (^)())block { [self addButtonWithTitle:title color:@"red" block:block atIndex:index]; } -- (void)setCancelButtonWithTitle:(NSString *)title atIndex:(NSInteger)index block:(void (^)())block -{ +- (void)setCancelButtonWithTitle:(NSString *)title atIndex:(NSInteger)index block:(void (^)())block { [self addButtonWithTitle:title color:@"black" block:block atIndex:index]; } -- (void)addButtonWithTitle:(NSString *)title atIndex:(NSInteger)index block:(void (^)())block -{ +- (void)addButtonWithTitle:(NSString *)title atIndex:(NSInteger)index block:(void (^)())block { [self addButtonWithTitle:title color:@"gray" block:block atIndex:index]; } +#pragma mark - Add button with block and animation completion block + +- (void)setDestructiveButtonWithTitle:(NSString *)title block:(void (^)())block completion:(void (^)())completionBlock +{ + [self addButtonWithTitle:title color:@"red" block:block atIndex:-1 completion:completionBlock]; +} + +- (void)setCancelButtonWithTitle:(NSString *)title block:(void (^)())block completion:(void (^)())completionBlock +{ + [self addButtonWithTitle:title color:@"black" block:block atIndex:-1 completion:completionBlock]; +} + +- (void)addButtonWithTitle:(NSString *)title block:(void (^)())block completion:(void (^)())completionBlock +{ + [self addButtonWithTitle:title color:@"gray" block:block atIndex:-1 completion:completionBlock]; +} + +#pragma mark - Add button at index with block and animation completion block + +- (void)setDestructiveButtonWithTitle:(NSString *)title atIndex:(NSInteger)index block:(void (^)())block completion:(void (^)())completionBlock +{ + [self addButtonWithTitle:title color:@"red" block:block atIndex:index completion:completionBlock]; +} + +- (void)setCancelButtonWithTitle:(NSString *)title atIndex:(NSInteger)index block:(void (^)())block completion:(void (^)())completionBlock +{ + [self addButtonWithTitle:title color:@"black" block:block atIndex:index completion:completionBlock]; +} + +- (void)addButtonWithTitle:(NSString *)title atIndex:(NSInteger)index block:(void (^)())block completion:(void (^)())completionBlock +{ + [self addButtonWithTitle:title color:@"gray" block:block atIndex:index completion:completionBlock]; +} + +# pragma mark - Show / Hide + - (void)showInView:(UIView *)view { NSUInteger i = 1; @@ -236,8 +282,9 @@ - (void)showInView:(UIView *)view [self retain]; } -- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated +- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated { + // Block Execution if (buttonIndex >= 0 && buttonIndex < [_blocks count]) { id obj = [[_blocks objectAtIndex: buttonIndex] objectAtIndex:0]; @@ -258,9 +305,22 @@ - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)anim _view.center = center; [[BlockBackground sharedInstance] reduceAlphaIfEmpty]; } completion:^(BOOL finished) { + + //Completion block execution + if (buttonIndex >= 0 && buttonIndex < [_completionBlocks count]) + { + id obj = [[_completionBlocks objectAtIndex: buttonIndex] objectAtIndex:0]; + if (![obj isEqual:[NSNull null]]) + { + ((void (^)())obj)(); + } + } + + // Release [[BlockBackground sharedInstance] removeView:_view]; [_view release]; _view = nil; [self autorelease]; + }]; } else @@ -273,7 +333,7 @@ - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)anim #pragma mark - Action -- (void)buttonClicked:(id)sender +- (void)buttonClicked:(id)sender { /* Run the button's block */ NSInteger buttonIndex = [(UIButton *)sender tag] - 1; diff --git a/BlockAlertsDemo/ToAddToYourProjects/BlockAlertView.h b/BlockAlertsDemo/ToAddToYourProjects/BlockAlertView.h index 15238de..ad54789 100755 --- a/BlockAlertsDemo/ToAddToYourProjects/BlockAlertView.h +++ b/BlockAlertsDemo/ToAddToYourProjects/BlockAlertView.h @@ -7,8 +7,9 @@ @interface BlockAlertView : NSObject { @protected - UIView *_view; + UIScrollView *_view; NSMutableArray *_blocks; + NSMutableArray *_completionBlocks; CGFloat _height; NSString *_title; NSString *_message; @@ -17,18 +18,33 @@ } + (BlockAlertView *)alertWithTitle:(NSString *)title message:(NSString *)message; ++ (BlockAlertView *)alertWithTitle:(NSString *)title + message:(NSString *)message + tintColor:(UIColor *)tintColor + textColor:(UIColor *)textColor; + (void)showInfoAlertWithTitle:(NSString *)title message:(NSString *)message; + (void)showErrorAlert:(NSError *)error; - (id)initWithTitle:(NSString *)title message:(NSString *)message; +- (id)initWithTitle:(NSString *)title + message:(NSString *)message + tintColor:(UIColor *)tintColor + textColor:(UIColor *)textColor; + +// Add button with block - (void)setDestructiveButtonWithTitle:(NSString *)title block:(void (^)())block; - (void)setCancelButtonWithTitle:(NSString *)title block:(void (^)())block; - (void)addButtonWithTitle:(NSString *)title block:(void (^)())block; +// Add button with block and animation completion block +- (void)setCancelButtonWithTitle:(NSString *)title block:(void (^)())block completion:(void (^)())completionBlock; +- (void)setDestructiveButtonWithTitle:(NSString *)title block:(void (^)())block completion:(void (^)())completionBlock; +- (void)addButtonWithTitle:(NSString *)title block:(void (^)())block completion:(void (^)())completionBlock; + // Images should be named in the form "alert-IDENTIFIER-button.png" -- (void)addButtonWithTitle:(NSString *)title imageIdentifier:(NSString*)identifier block:(void (^)())block; +- (void)addButtonWithTitle:(NSString *)title imageIdentifier:(NSString *)identifier block:(void (^)())block; - (void)addComponents:(CGRect)frame; @@ -37,8 +53,12 @@ - (void)setupDisplay; -@property (nonatomic, retain) UIImage *backgroundImage; -@property (nonatomic, readonly) UIView *view; -@property (nonatomic, readwrite) BOOL vignetteBackground; +@property(nonatomic, retain) UIImage *backgroundImage; +@property(nonatomic, readonly) UIScrollView *view; +@property(nonatomic, readwrite) BOOL vignetteBackground; +@property(nonatomic, retain) UIColor *tintColor; +@property(nonatomic, retain) UIColor *textColor; +@property(nonatomic, retain) UIFont *messageFont; +@property(nonatomic) BOOL allButtonInLine; @end diff --git a/BlockAlertsDemo/ToAddToYourProjects/BlockAlertView.m b/BlockAlertsDemo/ToAddToYourProjects/BlockAlertView.m index 5aa2e7f..d4d9103 100755 --- a/BlockAlertsDemo/ToAddToYourProjects/BlockAlertView.m +++ b/BlockAlertsDemo/ToAddToYourProjects/BlockAlertView.m @@ -12,6 +12,10 @@ @implementation BlockAlertView @synthesize view = _view; @synthesize backgroundImage = _backgroundImage; @synthesize vignetteBackground = _vignetteBackground; +@synthesize tintColor = _tintColor; +@synthesize textColor = _textColor; +@synthesize messageFont = _messageFont; +@synthesize allButtonInLine = _allButtonInLine; static UIImage *background = nil; static UIImage *backgroundlandscape = nil; @@ -19,18 +23,16 @@ @implementation BlockAlertView static UIFont *messageFont = nil; static UIFont *buttonFont = nil; - #pragma mark - init -+ (void)initialize -{ - if (self == [BlockAlertView class]) - { ++ (void)initialize { + if (self == [BlockAlertView class]) { background = [UIImage imageNamed:kAlertViewBackground]; background = [[background stretchableImageWithLeftCapWidth:0 topCapHeight:kAlertViewBackgroundCapHeight] retain]; backgroundlandscape = [UIImage imageNamed:kAlertViewBackgroundLandscape]; - backgroundlandscape = [[backgroundlandscape stretchableImageWithLeftCapWidth:0 topCapHeight:kAlertViewBackgroundCapHeight] retain]; + backgroundlandscape = + [[backgroundlandscape stretchableImageWithLeftCapWidth:0 topCapHeight:kAlertViewBackgroundCapHeight] retain]; titleFont = [kAlertViewTitleFont retain]; messageFont = [kAlertViewMessageFont retain]; @@ -38,22 +40,33 @@ + (void)initialize } } -+ (BlockAlertView *)alertWithTitle:(NSString *)title message:(NSString *)message -{ - return [[[BlockAlertView alloc] initWithTitle:title message:message] autorelease]; ++ (BlockAlertView *)alertWithTitle:(NSString *)title message:(NSString *)message { + return [[[BlockAlertView alloc] initWithTitle:title message:message tintColor:nil textColor:nil] autorelease]; } -+ (void)showInfoAlertWithTitle:(NSString *)title message:(NSString *)message -{ - BlockAlertView *alert = [[BlockAlertView alloc] initWithTitle:title message:message]; ++ (BlockAlertView *)alertWithTitle:(NSString *)title + message:(NSString *)message + tintColor:(UIColor *)tintColor + textColor:(UIColor *)textColor { + return [ + [[BlockAlertView alloc] initWithTitle:title message:message tintColor:tintColor textColor:textColor] autorelease]; +} + ++ (void)showInfoAlertWithTitle:(NSString *)title message:(NSString *)message { + BlockAlertView *alert = [[BlockAlertView alloc] initWithTitle:title message:message tintColor:nil textColor:nil]; [alert setCancelButtonWithTitle:NSLocalizedString(@"Dismiss", nil) block:nil]; [alert show]; [alert release]; } -+ (void)showErrorAlert:(NSError *)error -{ - BlockAlertView *alert = [[BlockAlertView alloc] initWithTitle:NSLocalizedString(@"Operation Failed", nil) message:[NSString stringWithFormat:NSLocalizedString(@"The operation did not complete successfully: %@", nil), error]]; ++ (void)showErrorAlert:(NSError *)error { + BlockAlertView *alert = [[BlockAlertView alloc] + initWithTitle:NSLocalizedString(@"Operation Failed", nil) + message:[NSString + stringWithFormat:NSLocalizedString(@"The operation did not complete successfully: %@", nil), + error] + tintColor:nil + textColor:nil]; [alert setCancelButtonWithTitle:@"Dismiss" block:nil]; [alert show]; [alert release]; @@ -63,27 +76,17 @@ + (void)showErrorAlert:(NSError *)error #pragma mark - NSObject - (void)addComponents:(CGRect)frame { - if (_title) - { - CGSize size; - if (IOS_LESS_THAN_7) { -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdeprecated-declarations" - size = [_title sizeWithFont:titleFont constrainedToSize:CGSizeMake(frame.size.width-kAlertViewBorder*2, 1000) lineBreakMode:NSLineBreakByWordWrapping]; -#pragma clang diagnostic pop - } - else { - NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; - paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping; - size = [_title boundingRectWithSize:CGSizeMake(frame.size.width-kAlertViewBorder*2, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSParagraphStyleAttributeName : paragraphStyle, NSFontAttributeName : titleFont} context:nil].size; - size = CGSizeMake(ceilf(size.width), ceilf(size.height)); - } + if (_title) { + CGSize size = [_title sizeWithFont:titleFont + constrainedToSize:CGSizeMake(frame.size.width - kAlertViewBorder * 2, 1000) + lineBreakMode:NSLineBreakByWordWrapping]; - UILabel *labelView = [[UILabel alloc] initWithFrame:CGRectMake(kAlertViewBorder, _height, frame.size.width-kAlertViewBorder*2, size.height)]; + UILabel *labelView = [[UILabel alloc] + initWithFrame:CGRectMake(kAlertViewBorder, _height, frame.size.width - kAlertViewBorder * 2, size.height)]; labelView.font = titleFont; labelView.numberOfLines = 0; labelView.lineBreakMode = NSLineBreakByWordWrapping; - labelView.textColor = kAlertViewTitleTextColor; + labelView.textColor = _textColor ? _textColor : kAlertViewTitleTextColor; labelView.backgroundColor = [UIColor clearColor]; labelView.textAlignment = NSTextAlignmentCenter; labelView.shadowColor = kAlertViewTitleShadowColor; @@ -95,27 +98,17 @@ - (void)addComponents:(CGRect)frame { _height += size.height + kAlertViewBorder; } - if (_message) - { - CGSize size; - if (IOS_LESS_THAN_7) { -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdeprecated-declarations" - size = [_message sizeWithFont:messageFont constrainedToSize:CGSizeMake(frame.size.width-kAlertViewBorder*2, 1000) lineBreakMode:NSLineBreakByWordWrapping]; -#pragma clang diagnostic pop - } - else { - NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; - paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping; - size = [_message boundingRectWithSize:CGSizeMake(frame.size.width-kAlertViewBorder*2, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSParagraphStyleAttributeName : paragraphStyle, NSFontAttributeName : messageFont} context:nil].size; - size = CGSizeMake(ceilf(size.width), ceilf(size.height)); - } + if (_message) { + CGSize size = [_message sizeWithFont:self.messageFont + constrainedToSize:CGSizeMake(frame.size.width - kAlertViewBorder * 2, 1000) + lineBreakMode:NSLineBreakByWordWrapping]; - UILabel *labelView = [[UILabel alloc] initWithFrame:CGRectMake(kAlertViewBorder, _height, frame.size.width-kAlertViewBorder*2, size.height)]; - labelView.font = messageFont; + UILabel *labelView = [[UILabel alloc] + initWithFrame:CGRectMake(kAlertViewBorder, _height, frame.size.width - kAlertViewBorder * 2, size.height)]; + labelView.font = self.messageFont; labelView.numberOfLines = 0; labelView.lineBreakMode = NSLineBreakByWordWrapping; - labelView.textColor = kAlertViewMessageTextColor; + labelView.textColor = _textColor ? _textColor : kAlertViewMessageTextColor; labelView.backgroundColor = [UIColor clearColor]; labelView.textAlignment = NSTextAlignmentCenter; labelView.shadowColor = kAlertViewMessageShadowColor; @@ -128,8 +121,7 @@ - (void)addComponents:(CGRect)frame { } } -- (void)setupDisplay -{ +- (void)setupDisplay { [[_view subviews] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { [obj removeFromSuperview]; }]; @@ -150,77 +142,95 @@ - (void)setupDisplay _height = kAlertViewBorder + 15; if (NeedsLandscapePhoneTweaks) { - _height -= 15; // landscape phones need to trimmed a bit + _height -= 15; // landscape phones need to trimmed a bit } - + [self addComponents:frame]; + + if (_shown) [self show]; +} - if (_shown) - [self show]; +- (id)initWithTitle:(NSString *)title message:(NSString *)message { + return [self initWithTitle:title message:message tintColor:nil textColor:nil]; } -- (id)initWithTitle:(NSString *)title message:(NSString *)message -{ +- (id)initWithTitle:(NSString *)title + message:(NSString *)message + tintColor:(UIColor *)tintColor + textColor:(UIColor *)textColor { self = [super init]; - if (self) - { + if (self) { _title = [title copy]; _message = [message copy]; - _view = [[UIView alloc] init]; + _view = [[UIScrollView alloc] init]; - _view.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; + _view.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin | + UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; _blocks = [[NSMutableArray alloc] init]; - if ([[[UIDevice currentDevice] systemVersion] compare:@"8.0" options:NSNumericSearch] == NSOrderedDescending){ + if ([[[UIDevice currentDevice] systemVersion] compare:@"8.0" options:NSNumericSearch] == NSOrderedDescending) { // don't register for notification, rotation is handled by iOS on 8+ - } - else { + } else { [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(setupDisplay) - name:UIApplicationDidChangeStatusBarOrientationNotification + selector:@selector(setupDisplay) + name:UIApplicationDidChangeStatusBarOrientationNotification object:nil]; } - if ([self class] == [BlockAlertView class]) - [self setupDisplay]; - + self.messageFont = messageFont; + if ([self class] == [BlockAlertView class]) [self setupDisplay]; + _tintColor = [tintColor retain]; + _textColor = [textColor retain]; _vignetteBackground = NO; } return self; } -- (void)dealloc -{ +- (void)dealloc { [_title release]; [_message release]; [_backgroundImage release]; [_view release]; [_blocks release]; + [_messageFont release]; + [_tintColor release]; + [_textColor release]; + [_completionBlocks release]; [super dealloc]; } /////////////////////////////////////////////////////////////////////////////////////////////////// #pragma mark - Public -- (void)addButtonWithTitle:(NSString *)title color:(NSString*)color block:(void (^)())block +- (void)addButtonWithTitle:(NSString *)title color:(NSString*)color block:(void (^)())block +{ + [self addButtonWithTitle:title color:color block:block completion:nil]; +} + +- (void)addButtonWithTitle:(NSString *)title color:(NSString*)color block:(void (^)())block completion:(void (^)())completionBlock { [_blocks addObject:[NSArray arrayWithObjects: block ? [[block copy] autorelease] : [NSNull null], title, color, nil]]; + [_completionBlocks addObject:[NSArray arrayWithObjects: + completionBlock ? [[completionBlock copy] autorelease] : [NSNull null], + title, + color, + nil]]; } -- (void)addButtonWithTitle:(NSString *)title block:(void (^)())block +- (void)addButtonWithTitle:(NSString *)title block:(void (^)())block { [self addButtonWithTitle:title color:@"gray" block:block]; } -- (void)setCancelButtonWithTitle:(NSString *)title block:(void (^)())block +- (void)setCancelButtonWithTitle:(NSString *)title block:(void (^)())block { [self addButtonWithTitle:title color:@"black" block:block]; } @@ -234,6 +244,25 @@ - (void)addButtonWithTitle:(NSString *)title imageIdentifier:(NSString*)identifi [self addButtonWithTitle:title color:identifier block:block]; } +- (void)addButtonWithTitle:(NSString *)title block:(void (^)())block completion:(void (^)())completionBlock +{ + [self addButtonWithTitle:title color:@"gray" block:block completion:completionBlock]; +} + +- (void)setCancelButtonWithTitle:(NSString *)title block:(void (^)())block completion:(void (^)())completionBlock +{ + [self addButtonWithTitle:title color:@"black" block:block completion:completionBlock]; +} + +- (void)setDestructiveButtonWithTitle:(NSString *)title block:(void (^)())block completion:(void (^)())completionBlock +{ + [self addButtonWithTitle:title color:@"red" block:block completion:completionBlock]; +} + +- (void)addButtonWithTitle:(NSString *)title imageIdentifier:(NSString*)identifier block:(void (^)())block completion:(void (^)())completionBlock { + [self addButtonWithTitle:title color:identifier block:block completion:completionBlock]; +} + - (void)show { _shown = YES; @@ -245,7 +274,7 @@ - (void)show NSArray *block = [_blocks objectAtIndex:i]; NSString *title = [block objectAtIndex:1]; NSString *color = [block objectAtIndex:2]; - + UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"alert-%@-button.png", color]]; image = [image stretchableImageWithLeftCapWidth:(int)(image.size.width+1)>>1 topCapHeight:0]; @@ -365,11 +394,11 @@ - (void)show [_view addSubview:button]; if (!isSecondButton) - _height += kAlertButtonHeight + kAlertViewBorder; + _height += kAlertButtonHeight + kAlertViewBorder; index++; } - + //_height += 10; // Margin for the shadow // not sure where this came from, but it's making things look strange (I don't see a shadow, either) if (_height < background.size.height) @@ -392,14 +421,14 @@ - (void)show frame.size.height = _height; _view.frame = frame; - + UIImageView *modalBackground = [[UIImageView alloc] initWithFrame:_view.bounds]; if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) - modalBackground.image = backgroundlandscape; + modalBackground.image = backgroundlandscape; else - modalBackground.image = background; - + modalBackground.image = background; + modalBackground.contentMode = UIViewContentModeScaleToFill; modalBackground.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; [_view insertSubview:modalBackground atIndex:0]; @@ -414,7 +443,7 @@ - (void)show [BlockBackground sharedInstance].vignetteBackground = _vignetteBackground; [[BlockBackground sharedInstance] addToMainWindow:_view]; - + __block CGPoint center = _view.center; center.y = floorf([BlockBackground sharedInstance].bounds.size.height * 0.5) + kAlertViewBounce; @@ -426,7 +455,7 @@ - (void)show animations:^{ [BlockBackground sharedInstance].alpha = 1.0f; _view.center = center; - } + } completion:^(BOOL finished) { if (_cancelBounce) return; @@ -436,7 +465,7 @@ - (void)show animations:^{ center.y -= kAlertViewBounce; _view.center = center; - } + } completion:^(BOOL finished) { [[NSNotificationCenter defaultCenter] postNotificationName:@"AlertViewFinishedAnimations" object:self]; }]; @@ -445,12 +474,13 @@ - (void)show [self retain]; } -- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated +- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated { _shown = NO; [[NSNotificationCenter defaultCenter] removeObserver:self]; + // Block execution if (buttonIndex >= 0 && buttonIndex < [_blocks count]) { id obj = [[_blocks objectAtIndex: buttonIndex] objectAtIndex:0]; @@ -469,21 +499,34 @@ - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)anim CGPoint center = _view.center; center.y += 20; _view.center = center; - } + } completion:^(BOOL finished) { [UIView animateWithDuration:0.4 - delay:0.0 + delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{ CGRect frame = _view.frame; frame.origin.y = -frame.size.height; _view.frame = frame; [[BlockBackground sharedInstance] reduceAlphaIfEmpty]; - } + } completion:^(BOOL finished) { + + // Completion block + if (buttonIndex >= 0 && buttonIndex < [_completionBlocks count]) + { + id obj = [[_completionBlocks objectAtIndex: buttonIndex] objectAtIndex:0]; + if (![obj isEqual:[NSNull null]]) + { + ((void (^)())obj)(); + } + } + + // Release [[BlockBackground sharedInstance] removeView:_view]; [_view release]; _view = nil; [self autorelease]; + }]; }]; } diff --git a/BlockAlertsDemo/images/ActionSheet/action-blue-button.png b/BlockAlertsDemo/images/ActionSheet/action-blue-button.png new file mode 100644 index 0000000..0ef7097 Binary files /dev/null and b/BlockAlertsDemo/images/ActionSheet/action-blue-button.png differ diff --git a/BlockAlertsDemo/images/ActionSheet/action-blue-button@2x.png b/BlockAlertsDemo/images/ActionSheet/action-blue-button@2x.png new file mode 100644 index 0000000..5642fd7 Binary files /dev/null and b/BlockAlertsDemo/images/ActionSheet/action-blue-button@2x.png differ diff --git a/BlockAlertsDemo/images/AlertView/alert-blue-button.png b/BlockAlertsDemo/images/AlertView/alert-blue-button.png new file mode 100644 index 0000000..0ad83b3 Binary files /dev/null and b/BlockAlertsDemo/images/AlertView/alert-blue-button.png differ diff --git a/BlockAlertsDemo/images/AlertView/alert-blue-button@2x.png b/BlockAlertsDemo/images/AlertView/alert-blue-button@2x.png new file mode 100644 index 0000000..78d332e Binary files /dev/null and b/BlockAlertsDemo/images/AlertView/alert-blue-button@2x.png differ