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
13 changes: 13 additions & 0 deletions CXAlertView/CXAlertBackgroundWindow.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// CXAlertBackgroundWindow.h
// CXAlertViewDemo
//
// Created by ChrisXu on 13/9/12.
// Copyright (c) 2013年 ChrisXu. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface CXAlertBackgroundWindow : UIWindow

@end
2 changes: 1 addition & 1 deletion CXAlertView/CXAlertButtonItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ typedef void(^CXAlertButtonHandler)(CXAlertView *alertView, CXAlertButtonItem *b

@interface CXAlertButtonItem : UIButton


@property (nonatomic, copy) NSString *title;
@property (nonatomic, assign) CXAlertViewButtonType type;
@property (nonatomic, copy) CXAlertButtonHandler action;
@property (nonatomic) BOOL defaultRightLineVisible;
@property (nonatomic, assign) BOOL bottomLineVisible;

@end
11 changes: 11 additions & 0 deletions CXAlertView/CXAlertButtonItem.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ - (void)drawRect:(CGRect)rect
CGContextAddLineToPoint(context, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
CGContextStrokePath(context);
}

if (self.bottomLineVisible) {
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, CGRectGetHeight(self.frame));
CGContextAddLineToPoint(context, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
CGContextStrokePath(context);
}
}

@end
1 change: 1 addition & 0 deletions CXAlertView/CXAlertView.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ typedef void(^CXAlertViewHandler)(CXAlertView *alertView);
@property (nonatomic, assign) CGFloat contentScrollViewMinHeight;
@property (nonatomic, assign) CGFloat bottomScrollViewHeight;
@property (nonatomic, assign) BOOL showButtonLine;
@property (nonatomic, assign) BOOL layoutButtonsVertical;
@property (nonatomic, assign) BOOL showBlurBackground;
// Create
- (id)initWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle;
Expand Down
142 changes: 88 additions & 54 deletions CXAlertView/CXAlertView.m
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ - (id)initWithFrame:(CGRect)frame
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.opaque = NO;
self.windowLevel = UIWindowLevelAlert - 1;

self.rootViewController = [[CXTempViewController alloc] init];
self.rootViewController.view.backgroundColor = [UIColor clearColor];
}
Expand All @@ -77,7 +77,7 @@ - (id)initWithFrame:(CGRect)frame
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();

[[UIColor colorWithWhite:0 alpha:0.5] set];
CGContextFillRect(context, self.bounds);
}
Expand Down Expand Up @@ -234,7 +234,7 @@ - (id)initWithTitle:(NSString *)title contentView:(UIView *)contentView cancelBu
self.buttonHeight = kDefaultNoButtonHeight;
_bottomScrollViewHeight = kDefaultNoButtonHeight;
}

_containerWidth = kDefaultContainerWidth;
_vericalPadding = kDefaultVericalPadding;
_topScrollViewMaxHeight = kDefaultTopScrollViewMaxHeight;
Expand Down Expand Up @@ -316,7 +316,7 @@ - (void)show

CXAlertViewController *viewController = [[CXAlertViewController alloc] initWithNibName:nil bundle:nil];
viewController.alertView = self;

if ([self.oldKeyWindow.rootViewController respondsToSelector:@selector(prefersStatusBarHidden)]) {
viewController.rootViewControllerPrefersStatusBarHidden = self.oldKeyWindow.rootViewController.prefersStatusBarHidden;
__cx_statsu_prefersStatusBarHidden = self.oldKeyWindow.rootViewController.prefersStatusBarHidden;
Expand Down Expand Up @@ -431,7 +431,7 @@ + (void)hideBackgroundAnimated:(BOOL)animated
__cx_alert_background_window = nil;
return;
}

[UIView animateWithDuration:0.3
animations:^{
__cx_alert_background_window.alpha = 0;
Expand Down Expand Up @@ -659,7 +659,7 @@ - (void)updateContentScrollView
- (void)updateBottomScrollView
{
_bottomScrollView.defaultTopLineVisible = _showButtonLine;

CGFloat y = 0;

y += [self heightForTopScrollView] + self.scrollViewPadding;
Expand All @@ -670,7 +670,7 @@ - (void)updateBottomScrollView

_bottomScrollView.backgroundColor = [UIColor clearColor];
_bottomScrollView.frame = CGRectMake( 0, y, self.containerWidth, [self heightForBottomScrollView]);

if (![_containerView.subviews containsObject:_bottomScrollView]) {
[_containerView addSubview:_bottomScrollView];
}
Expand Down Expand Up @@ -810,38 +810,53 @@ -(UIFont*)fontForButtonType:(CXAlertViewButtonType)type
return font;
}

- (CGRect)frameWithButtonTitile:(NSString *)title type:(CXAlertViewButtonType)type
-(CGFloat)heightThatFitsButton:(CXAlertButtonItem*)button
{
self.buttonHeight = kDefaultButtonHeight;
CGRect frame;
frame.size = CGSizeMake(self.containerWidth/2, self.buttonHeight);

if (_buttons.count == 1) {
frame.origin = CGPointMake(0., 0.);
frame.size.width = self.containerWidth;
}
else if (_buttons.count == 2) {
frame.origin = CGPointMake(self.containerWidth/2, 0.);
CGSize desiredSize=button.frame.size;
desiredSize.height=desiredSize.height;
desiredSize.width=desiredSize.width-20;

UIFont *fnt=[self fontForButtonType:button.type];
CGFloat btht=[button.title sizeWithFont:fnt constrainedToSize:desiredSize lineBreakMode:BT_LBM].height;
return ceil(btht)+22;
}

-(void)setMaxSizeForAllButtons
{
CGFloat maxHeight=22;
for(CXAlertButtonItem *button in self.buttons)
{
CGFloat ht=[self heightThatFitsButton:button];
if(ht>maxHeight)
{
maxHeight=ht;
}
}
else {
CXAlertButtonItem *lastButton = _buttons[_buttons.count - 2];
frame.origin = CGPointMake(CGRectGetMaxX(lastButton.frame), 0);

if (self.layoutButtonsVertical) {
CGFloat ht = 0;
for(CXAlertButtonItem *button in self.buttons) {
CGRect rect = button.frame;
rect.size.height = maxHeight;
rect.origin.y = ht;
ht += maxHeight;
button.frame = rect;
}
_bottomScrollView.contentSize = CGSizeMake(self.frame.size.width, ht);
_bottomScrollViewHeight = ht;
} else {


for(CXAlertButtonItem *button in self.buttons)
{
CGRect rect=button.frame;
rect.size.height=maxHeight;
button.frame=rect;
}

_bottomScrollView.contentSize = CGSizeMake( _bottomScrollView.contentSize.width, maxHeight);
_bottomScrollViewHeight=maxHeight;
}

UIFont *font = [self fontForButtonType:type];

#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_7_0
CGRect rect = [title boundingRectWithSize:CGSizeMake(frame.size.width, CGFLOAT_MAX) options:(NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:font} context:nil];
CGFloat newHeight = rect.size.height;
#else
CGFloat newHeight = [title sizeWithFont:font constrainedToSize:frame.size lineBreakMode:BT_LBM].height;
#endif

frame.size.height = MAX(newHeight, self.buttonHeight);

_maxButtonHeight = MAX(_maxButtonHeight, frame.size.height);

return frame;
}

- (void)addButtonWithTitle:(NSString *)title type:(CXAlertViewButtonType)type handler:(CXAlertButtonHandler)handler font:(UIFont *)font
Expand All @@ -851,46 +866,65 @@ - (void)addButtonWithTitle:(NSString *)title type:(CXAlertViewButtonType)type ha
button.action = handler;
button.type = type;
button.defaultRightLineVisible = _showButtonLine;
button.bottomLineVisible = NO;
[button setTitle:title forState:UIControlStateNormal];

button.titleLabel.textAlignment=TA_CENTER;
[button.titleLabel setNumberOfLines:0];
button.titleLabel.lineBreakMode=BT_LBM;
// [button setTitleEdgeInsets:UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0)];

CGFloat contentWidthOffset = 0.;
[_buttons addObject:button];

if ([_buttons count] == 1)
{

if ([_buttons count] == 0) {
button.defaultRightLineVisible = NO;

button.frame = [self frameWithButtonTitile:title type:type];
}
else
{
[_buttons addObject:button];
[self setMaxSizeForAllButtons];
} else if (self.layoutButtonsVertical) {
// Vertical buttons are simpler - just grow downwards instead
CXAlertButtonItem *lastButton = self.buttons.lastObject;
lastButton.bottomLineVisible = _showButtonLine;

CGRect lastFrame = lastButton.frame;
lastFrame.origin.y += self.buttonHeight;
button.frame = lastFrame;
button.defaultRightLineVisible = NO;

[self.buttons addObject:button];
[self setMaxSizeForAllButtons];
} else {
// correct first button
CXAlertButtonItem *firstButton = [_buttons objectAtIndex:0];
firstButton.defaultRightLineVisible = _showButtonLine;
CGFloat lastFirstButtonWidth = CGRectGetWidth(firstButton.frame);
CGRect newFrame = CGRectMake( 0, 0, self.containerWidth/2, CGRectGetHeight(firstButton.frame));
newFrame.origin.x = 0;
contentWidthOffset = lastFirstButtonWidth - CGRectGetWidth(newFrame);

newFrame.size.width = self.containerWidth/2;
[firstButton setNeedsDisplay];

CGFloat last_x = self.containerWidth/2 * [_buttons count];
button.frame = CGRectMake( last_x + self.containerWidth/2, 0, self.containerWidth/2, self.buttonHeight);
button.alpha = 0.;

[_buttons addObject:button];

void (^setButtonFrames)() = ^{
firstButton.frame = newFrame;
button.alpha = 1.;
button.frame = CGRectMake( last_x, 0, self.containerWidth/2, self.buttonHeight);
[self setMaxSizeForAllButtons];
};

if (self.isVisible) {
CGRect buttonFrame = [self frameWithButtonTitile:title type:type];
button.alpha = 0.;
button.frame = CGRectMake( 0, 0, CGRectGetWidth(buttonFrame), CGRectGetHeight(buttonFrame));
[UIView animateWithDuration:0.3 animations:^{
firstButton.frame = newFrame;
button.alpha = 1.;
button.frame = buttonFrame;
setButtonFrames();
}];
}
else {
firstButton.frame = newFrame;
button.alpha = 1.;
button.frame = [self frameWithButtonTitile:title type:type];
setButtonFrames();
}
}

Expand Down Expand Up @@ -1019,7 +1053,7 @@ - (void)setButtonHeight:(CGFloat)buttonHeight
return;
}
_buttonHeight = buttonHeight;

if (_bottomScrollViewHeight < _buttonHeight) {
_bottomScrollViewHeight = _buttonHeight;
[self updateBottomScrollView];
Expand Down