From c665a92fc6b3a7586326b45d9b031a3c0eda404a Mon Sep 17 00:00:00 2001 From: Greg Gardner Date: Wed, 9 Apr 2014 14:55:18 -0700 Subject: [PATCH 1/3] Added support for more than 2 buttons by stacking them, similar to the way that UIAlertView does it in IOS7. --- CXAlertView/CXAlertButtonItem.h | 1 + CXAlertView/CXAlertButtonItem.m | 10 +++++ CXAlertView/CXAlertView.m | 65 ++++++++++++++++++++++++++------- 3 files changed, 63 insertions(+), 13 deletions(-) diff --git a/CXAlertView/CXAlertButtonItem.h b/CXAlertView/CXAlertButtonItem.h index 0372d07..b821e57 100644 --- a/CXAlertView/CXAlertButtonItem.h +++ b/CXAlertView/CXAlertButtonItem.h @@ -25,5 +25,6 @@ typedef void(^CXAlertButtonHandler)(CXAlertView *alertView, CXAlertButtonItem *b @property (nonatomic, assign) CXAlertViewButtonType type; @property (nonatomic, copy) CXAlertButtonHandler action; @property (nonatomic) BOOL defaultRightLineVisible; +@property (nonatomic) BOOL defaultTopLineVisible; @end diff --git a/CXAlertView/CXAlertButtonItem.m b/CXAlertView/CXAlertButtonItem.m index 16329ad..03ee44c 100644 --- a/CXAlertView/CXAlertButtonItem.m +++ b/CXAlertView/CXAlertButtonItem.m @@ -32,6 +32,16 @@ - (void)drawRect:(CGRect)rect CGContextAddLineToPoint(context, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame)); CGContextStrokePath(context); } + if (_defaultTopLineVisible) { + CGContextRef context = UIGraphicsGetCurrentContext(); + CGContextClearRect(context, self.bounds); + + CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:0.671 green:0.675 blue:0.694 alpha:1.000].CGColor); + CGContextSetLineWidth(context, 1.0); + CGContextMoveToPoint(context, 0,0); + CGContextAddLineToPoint(context, CGRectGetWidth(self.frame), 0); + CGContextStrokePath(context); + } } @end diff --git a/CXAlertView/CXAlertView.m b/CXAlertView/CXAlertView.m index 33a25e8..6427f42 100644 --- a/CXAlertView/CXAlertView.m +++ b/CXAlertView/CXAlertView.m @@ -785,11 +785,12 @@ - (void)addButtonWithTitle:(NSString *)title type:(CXAlertViewButtonType)type ha button.defaultRightLineVisible = _showButtonLine; [button setTitle:title forState:UIControlStateNormal]; - button.titleLabel.textAlignment=UITextAlignmentCenter; + button.titleLabel.textAlignment=NSTextAlignmentCenter; [button.titleLabel setNumberOfLines:0]; button.titleLabel.lineBreakMode=BT_LBM; [button setTitleEdgeInsets:UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0)]; - + + self.buttonHeight = kDefaultButtonHeight; if ([_buttons count] == 0) { @@ -798,8 +799,11 @@ - (void)addButtonWithTitle:(NSString *)title type:(CXAlertViewButtonType)type ha [_buttons addObject:button]; [self setMaxSizeForAllButtons]; + + CGFloat newContentWidth = self.bottomScrollView.contentSize.width + CGRectGetWidth(button.frame); + _bottomScrollView.contentSize = CGSizeMake( newContentWidth, _bottomScrollView.contentSize.height); } - else + else if ([_buttons count] == 1) { // correct first button CXAlertButtonItem *firstButton = [_buttons objectAtIndex:0]; @@ -813,27 +817,62 @@ - (void)addButtonWithTitle:(NSString *)title type:(CXAlertViewButtonType)type ha button.alpha = 0.; [_buttons addObject:button]; + + void (^buttomFrameBlock)() = ^{ + firstButton.frame = newFrame; + button.alpha = 1.; + button.frame = CGRectMake( last_x, 0, self.containerWidth/2, self.buttonHeight); + [self setMaxSizeForAllButtons]; + }; if (self.isVisible) { [UIView animateWithDuration:0.3 animations:^{ - firstButton.frame = newFrame; - button.alpha = 1.; - button.frame = CGRectMake( last_x, 0, self.containerWidth/2, self.buttonHeight); - [self setMaxSizeForAllButtons]; + buttomFrameBlock(); }]; } else { - firstButton.frame = newFrame; - button.alpha = 1.; - button.frame = CGRectMake( last_x, 0, self.containerWidth/2, self.buttonHeight); + buttomFrameBlock(); + } + + CGFloat newContentWidth = self.bottomScrollView.contentSize.width + CGRectGetWidth(button.frame); + _bottomScrollView.contentSize = CGSizeMake( newContentWidth, _bottomScrollView.contentSize.height); + } + else // stack buttons if more than 2 + { + button.frame = CGRectMake(0, 0, self.containerWidth, self.buttonHeight); + button.alpha = 0.; + + [_buttons addObject:button]; + + void (^buttomFrameBlock)() = ^{ + CGFloat currentY = 0; + // correct existing buttons, set up new one as well + for (CXAlertButtonItem *button in _buttons) { + button.defaultRightLineVisible = NO; + button.defaultTopLineVisible = YES; + button.frame = CGRectMake(0, currentY, self.containerWidth, self.buttonHeight); + button.alpha = 1.; + [button setNeedsDisplay]; + currentY += self.buttonHeight; + } [self setMaxSizeForAllButtons]; + }; + + if (self.isVisible) { + [UIView animateWithDuration:0.3 animations:^{ + buttomFrameBlock(); + }]; } + else { + buttomFrameBlock(); + } + + _bottomScrollViewHeight = self.buttonHeight * _buttons.count; + _bottomScrollView.contentSize = CGSizeMake(self.bottomScrollView.contentSize.width, _bottomScrollViewHeight); + [self updateBottomScrollView]; } [_bottomScrollView addSubview:button]; - - CGFloat newContentWidth = self.bottomScrollView.contentSize.width + CGRectGetWidth(button.frame); - _bottomScrollView.contentSize = CGSizeMake( newContentWidth, _bottomScrollView.contentSize.height); } - (CXAlertButtonItem *)buttonItemWithType:(CXAlertViewButtonType)type font:(UIFont *)font From 7ad641641cbc0e30e2b893f6bfdec01304e5b1c9 Mon Sep 17 00:00:00 2001 From: Greg Gardner Date: Wed, 9 Apr 2014 16:06:56 -0700 Subject: [PATCH 2/3] Made button separator slightly thicker so that it will show up properly on iPad on IOS7. Renamed variable that was shadowing existing variable. Reverted change to deprecated enum since there is a pull request already to fix that issue. --- CXAlertView/CXAlertButtonItem.m | 2 +- CXAlertView/CXAlertView.m | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/CXAlertView/CXAlertButtonItem.m b/CXAlertView/CXAlertButtonItem.m index 03ee44c..390ec44 100644 --- a/CXAlertView/CXAlertButtonItem.m +++ b/CXAlertView/CXAlertButtonItem.m @@ -37,7 +37,7 @@ - (void)drawRect:(CGRect)rect CGContextClearRect(context, self.bounds); CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:0.671 green:0.675 blue:0.694 alpha:1.000].CGColor); - CGContextSetLineWidth(context, 1.0); + CGContextSetLineWidth(context, 2.0); CGContextMoveToPoint(context, 0,0); CGContextAddLineToPoint(context, CGRectGetWidth(self.frame), 0); CGContextStrokePath(context); diff --git a/CXAlertView/CXAlertView.m b/CXAlertView/CXAlertView.m index 6427f42..99c2a07 100644 --- a/CXAlertView/CXAlertView.m +++ b/CXAlertView/CXAlertView.m @@ -785,7 +785,7 @@ - (void)addButtonWithTitle:(NSString *)title type:(CXAlertViewButtonType)type ha button.defaultRightLineVisible = _showButtonLine; [button setTitle:title forState:UIControlStateNormal]; - button.titleLabel.textAlignment=NSTextAlignmentCenter; + button.titleLabel.textAlignment=UITextAlignmentCenter; [button.titleLabel setNumberOfLines:0]; button.titleLabel.lineBreakMode=BT_LBM; [button setTitleEdgeInsets:UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0)]; @@ -847,12 +847,12 @@ - (void)addButtonWithTitle:(NSString *)title type:(CXAlertViewButtonType)type ha void (^buttomFrameBlock)() = ^{ CGFloat currentY = 0; // correct existing buttons, set up new one as well - for (CXAlertButtonItem *button in _buttons) { - button.defaultRightLineVisible = NO; - button.defaultTopLineVisible = YES; - button.frame = CGRectMake(0, currentY, self.containerWidth, self.buttonHeight); - button.alpha = 1.; - [button setNeedsDisplay]; + for (CXAlertButtonItem *buttonItem in _buttons) { + buttonItem.defaultRightLineVisible = NO; + buttonItem.defaultTopLineVisible = YES; + buttonItem.frame = CGRectMake(0, currentY, self.containerWidth, self.buttonHeight); + buttonItem.alpha = 1.; + [buttonItem setNeedsDisplay]; currentY += self.buttonHeight; } [self setMaxSizeForAllButtons]; From 32d9685bf7779113cfca0f47b17258b1cec9935f Mon Sep 17 00:00:00 2001 From: Greg Gardner Date: Thu, 10 Apr 2014 18:06:40 -0700 Subject: [PATCH 3/3] Added flag to disable button stacking if wanted --- CXAlertView/CXAlertView.h | 1 + CXAlertView/CXAlertView.m | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CXAlertView/CXAlertView.h b/CXAlertView/CXAlertView.h index 14347ff..0f496ca 100644 --- a/CXAlertView/CXAlertView.h +++ b/CXAlertView/CXAlertView.h @@ -47,6 +47,7 @@ typedef void(^CXAlertViewHandler)(CXAlertView *alertView); @property (nonatomic, assign) CGFloat bottomScrollViewHeight; @property (nonatomic, assign) BOOL showButtonLine; @property (nonatomic, assign) BOOL showBlurBackground; +@property (nonatomic, assign) BOOL stackButtons; // Create - (id)initWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle; - (id)initWithTitle:(NSString *)title contentView:(UIView *)contentView cancelButtonTitle:(NSString *)cancelButtonTitle; diff --git a/CXAlertView/CXAlertView.m b/CXAlertView/CXAlertView.m index 99c2a07..dc770f7 100644 --- a/CXAlertView/CXAlertView.m +++ b/CXAlertView/CXAlertView.m @@ -197,6 +197,7 @@ - (id)initWithTitle:(NSString *)title contentView:(UIView *)contentView cancelBu _showButtonLine = YES; _showBlurBackground = YES; + _stackButtons = YES; [self setupScrollViews]; if (cancelButtonTitle) { [self addButtonWithTitle:cancelButtonTitle type:CXAlertViewButtonTypeCancel handler:^(CXAlertView *alertView, CXAlertButtonItem *button) { @@ -803,7 +804,7 @@ - (void)addButtonWithTitle:(NSString *)title type:(CXAlertViewButtonType)type ha CGFloat newContentWidth = self.bottomScrollView.contentSize.width + CGRectGetWidth(button.frame); _bottomScrollView.contentSize = CGSizeMake( newContentWidth, _bottomScrollView.contentSize.height); } - else if ([_buttons count] == 1) + else if ([_buttons count] == 1 || !_stackButtons) { // correct first button CXAlertButtonItem *firstButton = [_buttons objectAtIndex:0];