diff --git a/SCLAlertView-Objective-C.podspec b/SCLAlertView-Objective-C.podspec index ac40b16..1ce8100 100644 --- a/SCLAlertView-Objective-C.podspec +++ b/SCLAlertView-Objective-C.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |spec| spec.name = "SCLAlertView-Objective-C" - spec.version = "1.0.2" + spec.version = "1.0.3" spec.summary = "Beautiful animated Alert View. Written in Swift but ported to Objective-C" spec.homepage = "https://github.com/dogo/SCLAlertView" spec.screenshots = "https://raw.githubusercontent.com/dogo/SCLAlertView/master/ScreenShots/ScreenShot.png", "https://raw.githubusercontent.com/dogo/SCLAlertView/master/ScreenShots/ScreenShot2.png" diff --git a/SCLAlertView/SCLAlertView.h b/SCLAlertView/SCLAlertView.h index 0c2f386..36d2f63 100755 --- a/SCLAlertView/SCLAlertView.h +++ b/SCLAlertView/SCLAlertView.h @@ -50,7 +50,8 @@ typedef NS_ENUM(NSInteger, SCLAlertViewHideAnimation) SlideOutToLeft, SlideOutToRight, SlideOutToCenter, - SlideOutFromCenter + SlideOutFromCenter, + SimplyDisappear }; /** Alert show animation styles @@ -65,7 +66,8 @@ typedef NS_ENUM(NSInteger, SCLAlertViewShowAnimation) SlideInFromLeft, SlideInFromRight, SlideInFromCenter, - SlideInToCenter + SlideInToCenter, + SimplyAppear }; /** Alert background styles @@ -220,6 +222,12 @@ typedef NS_ENUM(NSInteger, SCLAlertViewBackground) */ @property (nonatomic) UIStatusBarStyle statusBarStyle; +/** Set horizontal alignment for buttons + * + * Horizontal aligment instead of vertically if YES + */ +@property (nonatomic) BOOL horizontalButtons; + /** Initialize SCLAlertView using a new window. * * Init with new window diff --git a/SCLAlertView/SCLAlertView.m b/SCLAlertView/SCLAlertView.m index 1839dc3..db88e0f 100755 --- a/SCLAlertView/SCLAlertView.m +++ b/SCLAlertView/SCLAlertView.m @@ -380,10 +380,17 @@ - (void)viewWillLayoutSubviews } // Buttons + CGFloat x = 12.0f; for (SCLButton *btn in _buttons) { - btn.frame = CGRectMake(12.0f, y, btn.frame.size.width, btn.frame.size.height); - y += btn.frame.size.height + 10.0f; + btn.frame = CGRectMake(x, y, btn.frame.size.width, btn.frame.size.height); + + // Add horizontal or vertical offset acording on _horizontalButtons parameter + if (_horizontalButtons) { + x += btn.frame.size.width + 10.0f; + } else { + y += btn.frame.size.height + 10.0f; + } } // Adapt window height according to icon size @@ -684,12 +691,24 @@ - (SCLButton *)addButton:(NSString *)title [btn setTitle:title forState:UIControlStateNormal]; btn.titleLabel.font = [UIFont fontWithName:_buttonsFontFamily size:_buttonsFontSize]; - // Update view height - self.windowHeight += (btn.frame.size.height + ADD_BUTTON_PADDING); - [_contentView addSubview:btn]; [_buttons addObject:btn]; + if (_horizontalButtons) { + // Update buttons width according to the number of buttons + for (SCLButton *bttn in _buttons) { + [bttn adjustWidthWithWindowWidth:self.windowWidth numberOfButtons:[_buttons count]]; + } + + // Update view height + if (!([_buttons count] > 1)) { + self.windowHeight += (btn.frame.size.height + ADD_BUTTON_PADDING); + } + } else { + // Update view height + self.windowHeight += (btn.frame.size.height + ADD_BUTTON_PADDING); + } + return btn; } @@ -1245,6 +1264,10 @@ - (void)showView case SlideInToCenter: [self slideInToCenter]; break; + + case SimplyAppear: + [self simplyAppear]; + break; } } @@ -1281,6 +1304,10 @@ - (void)hideView case SlideOutFromCenter: [self slideOutFromCenter]; break; + + case SimplyDisappear: + [self simplyDisappear]; + break; } if (_activityIndicatorView) @@ -1399,6 +1426,18 @@ - (void)slideOutFromCenter }]; } +- (void)simplyDisappear +{ + self.backgroundView.alpha = _backgroundOpacity; + self.view.alpha = 1.0f; + + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + self.backgroundView.alpha = 0.0f; + self.view.alpha = 0.0f; + }); +} + + #pragma mark - Show Animations - (void)fadeIn @@ -1572,6 +1611,18 @@ - (void)slideInToCenter }]; } +- (void)simplyAppear +{ + self.backgroundView.alpha = 0.0f; + self.view.alpha = 0.0f; + + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + self.backgroundView.alpha = _backgroundOpacity; + self.view.alpha = 1.0f; + }); +} + + @end @interface SCLALertViewTextFieldBuilder() diff --git a/SCLAlertView/SCLButton.h b/SCLAlertView/SCLButton.h old mode 100644 new mode 100755 index 8a46e46..b19ed77 --- a/SCLAlertView/SCLButton.h +++ b/SCLAlertView/SCLButton.h @@ -98,4 +98,12 @@ typedef NS_ENUM(NSInteger, SCLActionType) */ - (instancetype)initWithWindowWidth:(CGFloat)windowWidth; +/** Adjust width of the button according to the width of the alert and + * the number of buttons. Only used when buttons are horizontally aligned. + * + * @param windowWith The width of the alert. + * @param numberOfButtons The number of buttons in the alert. + */ +- (void)adjustWidthWithWindowWidth:(CGFloat)windowWidht numberOfButtons:(NSUInteger)numberOfButtons; + @end diff --git a/SCLAlertView/SCLButton.m b/SCLAlertView/SCLButton.m old mode 100644 new mode 100755 index cd84c15..2288ea6 --- a/SCLAlertView/SCLButton.m +++ b/SCLAlertView/SCLButton.m @@ -63,6 +63,14 @@ - (void)setupWithWindowWidth:(CGFloat)windowWidth self.layer.cornerRadius = 3.0f; } +- (void)adjustWidthWithWindowWidth:(CGFloat)windowWidht numberOfButtons:(NSUInteger)numberOfButtons +{ + CGFloat allButtonsWidth = windowWidht - (MARGIN_BUTTON * 2); + CGFloat buttonWidth = (allButtonsWidth - ((numberOfButtons - 1) * 10)) / numberOfButtons; + + self.frame = CGRectMake(0.0f, 0.0f, buttonWidth, MIN_HEIGHT); +} + - (void)setTitle:(NSString *)title forState:(UIControlState)state { [super setTitle:title forState:state]; diff --git a/SCLAlertViewExample/Base.lproj/Storyboard.storyboard b/SCLAlertViewExample/Base.lproj/Storyboard.storyboard index 7c01a6e..575a677 100644 --- a/SCLAlertViewExample/Base.lproj/Storyboard.storyboard +++ b/SCLAlertViewExample/Base.lproj/Storyboard.storyboard @@ -1,8 +1,8 @@ - + - + @@ -14,189 +14,227 @@ - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - + + + + + + diff --git a/SCLAlertViewExample/ViewController.h b/SCLAlertViewExample/ViewController.h index 81de180..8aff9e1 100644 --- a/SCLAlertViewExample/ViewController.h +++ b/SCLAlertViewExample/ViewController.h @@ -11,13 +11,17 @@ @interface ViewController : UIViewController - (IBAction)showSuccess:(id)sender; +- (IBAction)showSuccessWithHorizontalButtons:(id)sender; - (IBAction)showError:(id)sender; - (IBAction)showNotice:(id)sender; - (IBAction)showWarning:(id)sender; - (IBAction)showInfo:(id)sender; - (IBAction)showEdit:(id)sender; +- (IBAction)showEditWithHorizontalButtons:(id)sender; +- (IBAction)ShowAdvancedWithHorizontalButtons:(id)sender; - (IBAction)showCustom:(id)sender; - (IBAction)showValidation:(id)sender; +- (IBAction)showValidationWithHorizontalButtons:(id)sender; - (IBAction)showWaiting:(id)sender; @end diff --git a/SCLAlertViewExample/ViewController.m b/SCLAlertViewExample/ViewController.m index c3e8e2a..53c2eb6 100644 --- a/SCLAlertViewExample/ViewController.m +++ b/SCLAlertViewExample/ViewController.m @@ -70,6 +70,33 @@ - (IBAction)showSuccess:(id)sender [alert showSuccess:kSuccessTitle subTitle:kSubtitle closeButtonTitle:kButtonTitle duration:0.0f]; } +- (IBAction)showSuccessWithHorizontalButtons:(id)sender { + SCLAlertView *alert = [[SCLAlertView alloc] initWithNewWindow]; + [alert setHorizontalButtons:YES]; + + SCLButton *button = [alert addButton:@"First Button" target:self selector:@selector(firstButton)]; + + button.buttonFormatBlock = ^NSDictionary* (void) + { + NSMutableDictionary *buttonConfig = [[NSMutableDictionary alloc] init]; + + buttonConfig[@"backgroundColor"] = [UIColor whiteColor]; + buttonConfig[@"textColor"] = [UIColor blackColor]; + buttonConfig[@"borderWidth"] = @2.0f; + buttonConfig[@"borderColor"] = [UIColor greenColor]; + + return buttonConfig; + }; + + [alert addButton:@"Second Button" actionBlock:^(void) { + NSLog(@"Second button tapped"); + }]; + + alert.soundURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/right_answer.mp3", [NSBundle mainBundle].resourcePath]]; + + [alert showSuccess:kSuccessTitle subTitle:kSubtitle closeButtonTitle:kButtonTitle duration:0.0f]; +} + - (IBAction)showError:(id)sender { SCLAlertView *alert = [[SCLAlertView alloc] init]; @@ -84,7 +111,7 @@ - (IBAction)showNotice:(id)sender SCLAlertView *alert = [[SCLAlertView alloc] init]; alert.backgroundType = Blur; - + alert.showAnimationType = FadeIn; [alert showNotice:self title:kNoticeTitle subTitle:@"You've just displayed this awesome Pop Up View with blur effect" closeButtonTitle:kButtonTitle duration:0.0f]; } @@ -100,7 +127,7 @@ - (IBAction)showInfo:(id)sender SCLAlertView *alert = [[SCLAlertView alloc] init]; alert.shouldDismissOnTapOutside = YES; - + alert.showAnimationType = SimplyAppear; [alert alertIsDismissed:^{ NSLog(@"SCLAlertView dismissed!"); }]; @@ -113,7 +140,21 @@ - (IBAction)showEdit:(id)sender SCLAlertView *alert = [[SCLAlertView alloc] init]; SCLTextView *textField = [alert addTextField:@"Enter your name"]; + alert.hideAnimationType = SimplyDisappear; + [alert addButton:@"Show Name" actionBlock:^(void) { + NSLog(@"Text value: %@", textField.text); + }]; + + [alert showEdit:self title:kInfoTitle subTitle:kSubtitle closeButtonTitle:kButtonTitle duration:0.0f]; +} + +- (IBAction)showEditWithHorizontalButtons:(id)sender +{ + SCLAlertView *alert = [[SCLAlertView alloc] init]; + [alert setHorizontalButtons:YES]; + SCLTextView *textField = [alert addTextField:@"Enter your name"]; + alert.hideAnimationType = SimplyDisappear; [alert addButton:@"Show Name" actionBlock:^(void) { NSLog(@"Text value: %@", textField.text); }]; @@ -174,6 +215,60 @@ - (IBAction)showAdvanced:(id)sender [alert showTitle:self title:@"Congratulations" subTitle:kAttributeTitle style:Success closeButtonTitle:@"Done" duration:0.0f]; } +- (IBAction)ShowAdvancedWithHorizontalButtons:(id)sender +{ + SCLAlertView *alert = [[SCLAlertView alloc] init]; + [alert setHorizontalButtons:YES]; + + alert.backgroundViewColor = [UIColor cyanColor]; + + [alert setTitleFontFamily:@"Superclarendon" withSize:20.0f]; + [alert setBodyTextFontFamily:@"TrebuchetMS" withSize:14.0f]; + [alert setButtonsTextFontFamily:@"Baskerville" withSize:14.0f]; + + [alert addButton:@"First Button" target:self selector:@selector(firstButton)]; + + [alert addButton:@"Second Button" actionBlock:^(void) { + NSLog(@"Second button tapped"); + }]; + + SCLTextView *textField = [alert addTextField:@"Enter your name"]; + + [alert addButton:@"Show Name" actionBlock:^(void) { + NSLog(@"Text value: %@", textField.text); + }]; + + alert.completeButtonFormatBlock = ^NSDictionary* (void) + { + NSMutableDictionary *buttonConfig = [[NSMutableDictionary alloc] init]; + + buttonConfig[@"backgroundColor"] = [UIColor greenColor]; + buttonConfig[@"borderColor"] = [UIColor blackColor]; + buttonConfig[@"borderWidth"] = @"1.0f"; + buttonConfig[@"textColor"] = [UIColor blackColor]; + + return buttonConfig; + }; + + alert.attributedFormatBlock = ^NSAttributedString* (NSString *value) + { + NSMutableAttributedString *subTitle = [[NSMutableAttributedString alloc]initWithString:value]; + + NSRange redRange = [value rangeOfString:@"Attributed" options:NSCaseInsensitiveSearch]; + [subTitle addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:redRange]; + + NSRange greenRange = [value rangeOfString:@"successfully" options:NSCaseInsensitiveSearch]; + [subTitle addAttribute:NSForegroundColorAttributeName value:[UIColor brownColor] range:greenRange]; + + NSRange underline = [value rangeOfString:@"completed" options:NSCaseInsensitiveSearch]; + [subTitle addAttributes:@{NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle)} range:underline]; + + return subTitle; + }; + + [alert showTitle:self title:@"Congratulations" subTitle:kAttributeTitle style:Success closeButtonTitle:@"Done" duration:0.0f]; +} + - (IBAction)showWithDuration:(id)sender { SCLAlertView *alert = [[SCLAlertView alloc] init]; @@ -241,6 +336,59 @@ - (IBAction)showValidation:(id)sender [alert showEdit:self title:@"Validation" subTitle:@"Ensure the data is correct before dismissing!" closeButtonTitle:@"Cancel" duration:0]; } +- (IBAction)showValidationWithHorizontalButtons:(id)sender +{ + SCLAlertView *alert = [[SCLAlertView alloc] init]; + [alert setHorizontalButtons:YES]; + + SCLTextView *evenField = [alert addTextField:@"Enter an even number"]; + evenField.keyboardType = UIKeyboardTypeNumberPad; + + SCLTextView *oddField = [alert addTextField:@"Enter an odd number"]; + oddField.keyboardType = UIKeyboardTypeNumberPad; + + [alert addButton:@"Test Validation" validationBlock:^BOOL{ + if (evenField.text.length == 0) + { + [[[UIAlertView alloc] initWithTitle:@"Whoops!" message:@"You forgot to add an even number." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show]; + [evenField becomeFirstResponder]; + return NO; + } + + if (oddField.text.length == 0) + { + [[[UIAlertView alloc] initWithTitle:@"Whoops!" message:@"You forgot to add an odd number." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show]; + [oddField becomeFirstResponder]; + return NO; + } + + NSInteger evenFieldEntry = (evenField.text).integerValue; + BOOL evenFieldPassedValidation = evenFieldEntry % 2 == 0; + + if (!evenFieldPassedValidation) + { + [[[UIAlertView alloc] initWithTitle:@"Whoops!" message:@"That is not an even number." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show]; + [evenField becomeFirstResponder]; + return NO; + } + + NSInteger oddFieldEntry = (oddField.text).integerValue; + BOOL oddFieldPassedValidation = oddFieldEntry % 2 == 1; + + if (!oddFieldPassedValidation) + { + [[[UIAlertView alloc] initWithTitle:@"Whoops!" message:@"That is not an odd number." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show]; + [oddField becomeFirstResponder]; + return NO; + } + return YES; + } actionBlock:^{ + [[[UIAlertView alloc] initWithTitle:@"Great Job!" message:@"Thanks for playing." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show]; + }]; + + [alert showEdit:self title:@"Validation" subTitle:@"Ensure the data is correct before dismissing!" closeButtonTitle:@"Cancel" duration:0]; +} + - (IBAction)showWaiting:(id)sender { SCLAlertView *alert = [[SCLAlertView alloc] init];