Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for text input #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion SIAlertView/SIAlertView.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ typedef NS_ENUM(NSInteger, SIAlertViewTransitionStyle) {
@class SIAlertView;
typedef void(^SIAlertViewHandler)(SIAlertView *alertView);

@interface SIAlertView : UIView
@interface SIAlertView : UIView <UITextFieldDelegate>

@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *message;
@property (nonatomic, copy) NSString *input;

@property (nonatomic, assign) SIAlertViewTransitionStyle transitionStyle; // default is SIAlertViewTransitionStyleSlideFromBottom
@property (nonatomic, assign) SIAlertViewBackgroundStyle backgroundStyle; // default is SIAlertViewButtonTypeGradient
Expand All @@ -53,14 +54,17 @@ typedef void(^SIAlertViewHandler)(SIAlertView *alertView);
@property (nonatomic, strong) UIColor *viewBackgroundColor NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
@property (nonatomic, strong) UIColor *titleColor NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
@property (nonatomic, strong) UIColor *messageColor NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
@property (nonatomic, strong) UIColor *inputColor NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
@property (nonatomic, strong) UIFont *titleFont NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
@property (nonatomic, strong) UIFont *messageFont NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
@property (nonatomic, strong) UIFont *inputFont NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
@property (nonatomic, strong) UIFont *buttonFont NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
@property (nonatomic, assign) CGFloat cornerRadius NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; // default is 2.0
@property (nonatomic, assign) CGFloat shadowRadius NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; // default is 8.0

- (id)initWithTitle:(NSString *)title andMessage:(NSString *)message;
- (void)addButtonWithTitle:(NSString *)title type:(SIAlertViewButtonType)type handler:(SIAlertViewHandler)handler;
- (void)addInputFieldWithPlaceholder:(NSString*)placeholder andHandler:(SIAlertViewHandler)handler;

- (void)show;
- (void)dismissAnimated:(BOOL)animated;
Expand Down
102 changes: 101 additions & 1 deletion SIAlertView/SIAlertView.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ @interface SIAlertView ()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *messageLabel;
@property (nonatomic, strong) UITextField *inputField;
@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) NSMutableArray *buttons;

Expand Down Expand Up @@ -126,6 +127,7 @@ - (void)drawRect:(CGRect)rect
@interface SIAlertItem : NSObject

@property (nonatomic, copy) NSString *title;
@property (nonatomic, strong) NSString *input;
@property (nonatomic, assign) SIAlertViewButtonType type;
@property (nonatomic, copy) SIAlertViewHandler action;

Expand Down Expand Up @@ -194,8 +196,10 @@ + (void)initialize
appearance.viewBackgroundColor = [UIColor whiteColor];
appearance.titleColor = [UIColor blackColor];
appearance.messageColor = [UIColor darkGrayColor];
appearance.inputColor = [UIColor lightGrayColor];
appearance.titleFont = [UIFont boldSystemFontOfSize:20];
appearance.messageFont = [UIFont systemFontOfSize:16];
appearance.inputFont = [UIFont systemFontOfSize:16];
appearance.buttonFont = [UIFont systemFontOfSize:[UIFont buttonFontSize]];
appearance.cornerRadius = 2;
appearance.shadowRadius = 8;
Expand Down Expand Up @@ -292,6 +296,12 @@ - (void)setMessage:(NSString *)message
[self invaliadateLayout];
}

- (void)setInput:(NSString *)input
{
_input = input;
[self invaliadateLayout];
}

#pragma mark - Public

- (void)addButtonWithTitle:(NSString *)title type:(SIAlertViewButtonType)type handler:(SIAlertViewHandler)handler
Expand All @@ -303,6 +313,12 @@ - (void)addButtonWithTitle:(NSString *)title type:(SIAlertViewButtonType)type ha
[self.items addObject:item];
}

- (void)addInputFieldWithPlaceholder:(NSString *)placeholder andHandler:(SIAlertViewHandler)handler
{
_input = placeholder;
[self invaliadateLayout];
}

- (void)show
{
self.oldKeyWindow = [[UIApplication sharedApplication] keyWindow];
Expand Down Expand Up @@ -668,6 +684,15 @@ - (void)validateLayout
self.messageLabel.frame = CGRectMake(CONTENT_PADDING_LEFT, y, self.containerView.bounds.size.width - CONTENT_PADDING_LEFT * 2, height);
y += height;
}
if (self.inputField) {
if (y > CONTENT_PADDING_TOP) {
y += GAP;
}
self.inputField.placeholder = self.input;
CGFloat height = [self heightForInputField];
self.inputField.frame = CGRectMake(CONTENT_PADDING_LEFT, y, self.containerView.bounds.size.width - CONTENT_PADDING_LEFT * 2, height);
y += height;
}
if (self.items.count > 0) {
if (y > CONTENT_PADDING_TOP) {
y += GAP;
Expand Down Expand Up @@ -707,6 +732,12 @@ - (CGFloat)preferredHeight
}
height += [self heightForMessageLabel];
}
if (self.inputField) {
if (height > CONTENT_PADDING_TOP) {
height += GAP;
}
height += [self heightForInputField];
}
if (self.items.count > 0) {
if (height > CONTENT_PADDING_TOP) {
height += GAP;
Expand Down Expand Up @@ -755,13 +786,19 @@ - (CGFloat)heightForMessageLabel
return minHeight;
}

- (CGFloat)heightForInputField
{
return 2 * self.inputField.font.lineHeight;
}

#pragma mark - Setup

- (void)setup
{
[self setupContainerView];
[self updateTitleLabel];
[self updateMessageLabel];
[self updateInputField];
[self setupButtons];
[self invaliadateLayout];
}
Expand All @@ -772,6 +809,7 @@ - (void)teardown
self.containerView = nil;
self.titleLabel = nil;
self.messageLabel = nil;
self.inputField = nil;
[self.buttons removeAllObjects];
[self.alertWindow removeFromSuperview];
self.alertWindow = nil;
Expand Down Expand Up @@ -839,6 +877,38 @@ - (void)updateMessageLabel
[self invaliadateLayout];
}

- (void)updateInputField
{
if (self.input) {
if (!self.inputField) {
self.inputField = [[UITextField alloc] initWithFrame:self.bounds];
self.inputField.textAlignment = NSTextAlignmentCenter;
self.inputField.font = self.inputFont;
self.inputField.textColor = self.inputColor;
self.inputField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
self.inputField.autocorrectionType = UITextAutocorrectionTypeNo;

UIImage *normalImage = [UIImage imageNamed:@"SIAlertView.bundle/button-default"];
CGFloat hInset = floorf(normalImage.size.width / 2);
CGFloat vInset = floorf(normalImage.size.height / 2);
UIEdgeInsets insets = UIEdgeInsetsMake(vInset, hInset, vInset, hInset);
normalImage = [normalImage resizableImageWithCapInsets:insets];
[self.inputField setBackground:normalImage];

self.inputField.delegate = self;
[self.containerView addSubview:self.inputField];
#if DEBUG_LAYOUT
self.inputField.backgroundColor = [UIColor redColor];
#endif
}
self.inputField.text = self.input;
} else {
[self.inputField removeFromSuperview];
self.inputField = nil;
}
[self invaliadateLayout];
}

- (void)setupButtons
{
self.buttons = [[NSMutableArray alloc] initWithCapacity:self.items.count];
Expand All @@ -859,6 +929,7 @@ - (UIButton *)buttonForItemIndex:(NSUInteger)index
[button setTitle:item.title forState:UIControlStateNormal];
UIImage *normalImage = nil;
UIImage *highlightedImage = nil;

switch (item.type) {
case SIAlertViewButtonTypeCancel:
normalImage = [UIImage imageNamed:@"SIAlertView.bundle/button-cancel"];
Expand Down Expand Up @@ -887,20 +958,30 @@ - (UIButton *)buttonForItemIndex:(NSUInteger)index
highlightedImage = [highlightedImage resizableImageWithCapInsets:insets];
[button setBackgroundImage:normalImage forState:UIControlStateNormal];
[button setBackgroundImage:highlightedImage forState:UIControlStateHighlighted];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

return button;
}

#pragma mark - UITextField Delegate

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
self.inputField.text = @"";
}

#pragma mark - Actions

- (void)buttonAction:(UIButton *)button
{
self.input = self.inputField.text;

[SIAlertView setAnimating:YES]; // set this flag to YES in order to prevent showing another alert in action block
SIAlertItem *item = self.items[button.tag];
if (item.action) {
item.action(self);
}

[self dismissAnimated:YES];
}

Expand Down Expand Up @@ -945,6 +1026,16 @@ - (void)setMessageFont:(UIFont *)messageFont
[self invaliadateLayout];
}

- (void)setInputFont:(UIFont *)inputFont
{
if (_inputFont == inputFont) {
return;
}
_inputFont = inputFont;
self.inputField.font = inputFont;
[self invaliadateLayout];
}

- (void)setTitleColor:(UIColor *)titleColor
{
if (_titleColor == titleColor) {
Expand All @@ -963,6 +1054,15 @@ - (void)setMessageColor:(UIColor *)messageColor
self.messageLabel.textColor = messageColor;
}

- (void)setInputColor:(UIColor *)inputColor
{
if (_inputColor == inputColor) {
return;
}
_inputColor = inputColor;
self.inputField.textColor = inputColor;
}

- (void)setButtonFont:(UIFont *)buttonFont
{
if (_buttonFont == buttonFont) {
Expand Down
39 changes: 39 additions & 0 deletions SIAlertViewExample/SIAlertViewExample/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,45 @@ - (IBAction)alert4:(id)sender
[self alert3:nil];
}

- (IBAction)alert5:(id)sender
{
SIAlertView *alertView = [[SIAlertView alloc] initWithTitle:@"Title5" andMessage:@"Message5"];
[alertView addButtonWithTitle:@"Cancel"
type:SIAlertViewButtonTypeCancel
handler:^(SIAlertView *alertView) {
NSLog(@"Input Received: %@", alertView.input);
NSLog(@"Cancel Clicked");
}];
[alertView addButtonWithTitle:@"OK"
type:SIAlertViewButtonTypeDefault
handler:^(SIAlertView *alertView) {
NSLog(@"Input Received: %@", alertView.input);
NSLog(@"OK Clicked");
}];
[alertView addInputFieldWithPlaceholder:@"Placeholder" andHandler:^(SIAlertView *alertView){
NSLog(@"handler for input field");
}];
alertView.titleColor = [UIColor blueColor];
alertView.cornerRadius = 10;
alertView.buttonFont = [UIFont boldSystemFontOfSize:15];
alertView.transitionStyle = SIAlertViewTransitionStyleBounce;

alertView.willShowHandler = ^(SIAlertView *alertView) {
NSLog(@"%@, willShowHandler5", alertView);
};
alertView.didShowHandler = ^(SIAlertView *alertView) {
NSLog(@"%@, didShowHandler5", alertView);
};
alertView.willDismissHandler = ^(SIAlertView *alertView) {
NSLog(@"%@, willDismissHandler5", alertView);
};
alertView.didDismissHandler = ^(SIAlertView *alertView) {
NSLog(@"%@, didDismissHandler5", alertView);
};

[alertView show];
}

- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="2.0" toolsVersion="3084" systemVersion="12D78" targetRuntime="iOS.CocoaTouch.iPad" propertyAccessControl="none" initialViewController="2">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="2.0" toolsVersion="3084" systemVersion="13A497d" targetRuntime="iOS.CocoaTouch.iPad" propertyAccessControl="none" initialViewController="2">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="2083"/>
</dependencies>
Expand Down Expand Up @@ -58,7 +58,7 @@
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" id="b8f-lj-NAQ">
<rect key="frame" x="348" y="238" width="73" height="44"/>
<rect key="frame" x="348" y="289" width="73" height="44"/>
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="15"/>
<state key="normal" title="1+2+3">
Expand All @@ -72,6 +72,21 @@
<action selector="alert4:" destination="2" eventType="touchUpInside" id="rPK-cd-51h"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" id="wvI-mB-hZb">
<rect key="frame" x="348" y="238" width="73" height="44"/>
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="15"/>
<state key="normal" title="Alert4">
<color key="titleColor" red="0.19607843459999999" green="0.30980393290000002" blue="0.52156865600000002" alpha="1" colorSpace="calibratedRGB"/>
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<state key="highlighted">
<color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="alert5:" destination="2" eventType="touchUpInside" id="uRz-dG-TfK"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
</view>
Expand All @@ -89,6 +104,7 @@
<relationship kind="action" name="alert2:"/>
<relationship kind="action" name="alert3:"/>
<relationship kind="action" name="alert4:"/>
<relationship kind="action" name="alert5:"/>
</relationships>
</class>
</classes>
Expand Down