diff --git a/BlockAlertsDemo.xcodeproj/project.pbxproj b/BlockAlertsDemo.xcodeproj/project.pbxproj index ff0c019..6d54214 100644 --- a/BlockAlertsDemo.xcodeproj/project.pbxproj +++ b/BlockAlertsDemo.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + B5B0620C14E95174007F2C78 /* BlockTextPromptAlertView.m in Sources */ = {isa = PBXBuildFile; fileRef = B5B0620B14E95174007F2C78 /* BlockTextPromptAlertView.m */; }; EC90169F14BB629F00EF52E1 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = EC90169E14BB629F00EF52E1 /* UIKit.framework */; }; EC9016A114BB629F00EF52E1 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = EC9016A014BB629F00EF52E1 /* Foundation.framework */; }; EC9016A314BB629F00EF52E1 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = EC9016A214BB629F00EF52E1 /* CoreGraphics.framework */; }; @@ -49,6 +50,8 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + B5B0620A14E95174007F2C78 /* BlockTextPromptAlertView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BlockTextPromptAlertView.h; sourceTree = ""; }; + B5B0620B14E95174007F2C78 /* BlockTextPromptAlertView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BlockTextPromptAlertView.m; sourceTree = ""; }; EC90169A14BB629F00EF52E1 /* BlockAlertsDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = BlockAlertsDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; EC90169E14BB629F00EF52E1 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; }; EC9016A014BB629F00EF52E1 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; @@ -225,6 +228,8 @@ EC9016C314BB62ED00EF52E1 /* BlockActionSheet.m */, EC9016C414BB62ED00EF52E1 /* BlockAlertView.h */, EC9016C514BB62ED00EF52E1 /* BlockAlertView.m */, + B5B0620A14E95174007F2C78 /* BlockTextPromptAlertView.h */, + B5B0620B14E95174007F2C78 /* BlockTextPromptAlertView.m */, ); name = "Needed for your project"; sourceTree = ""; @@ -326,6 +331,7 @@ EC9016C814BB62ED00EF52E1 /* BlockActionSheet.m in Sources */, EC9016C914BB62ED00EF52E1 /* BlockAlertView.m in Sources */, EC9016CA14BB62ED00EF52E1 /* BlockBackground.m in Sources */, + B5B0620C14E95174007F2C78 /* BlockTextPromptAlertView.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/BlockAlertsDemo/BlockAlertView.h b/BlockAlertsDemo/BlockAlertView.h index 9a6a443..f801510 100755 --- a/BlockAlertsDemo/BlockAlertView.h +++ b/BlockAlertsDemo/BlockAlertView.h @@ -6,7 +6,7 @@ #import @interface BlockAlertView : NSObject { -@private +@protected UIView *_view; NSMutableArray *_blocks; CGFloat _height; diff --git a/BlockAlertsDemo/BlockTextPromptAlertView.h b/BlockAlertsDemo/BlockTextPromptAlertView.h new file mode 100644 index 0000000..b3f7636 --- /dev/null +++ b/BlockAlertsDemo/BlockTextPromptAlertView.h @@ -0,0 +1,30 @@ +// +// BlockTextPromptAlertView.h +// BlockAlertsDemo +// +// Created by Barrett Jacobsen on 2/13/12. +// Copyright (c) 2012 Barrett Jacobsen. All rights reserved. +// + +#import "BlockAlertView.h" + +@interface BlockTextPromptAlertView : BlockAlertView { + + NSCharacterSet *unacceptedInput; + NSInteger maxLength; +} + +@property (nonatomic, retain) UITextField *textField; + +@property (nonatomic, readonly) NSString* enteredText; + ++ (BlockTextPromptAlertView *)promptWithTitle:(NSString *)title message:(NSString *)message defaultText:(NSString*)defaultText; + + +- (id)initWithTitle:(NSString *)title message:(NSString *)message defaultText:(NSString*)defaultText; + + +- (void)setAllowableCharacters:(NSString*)accepted; +- (void)setMaxLength:(NSInteger)max; + +@end diff --git a/BlockAlertsDemo/BlockTextPromptAlertView.m b/BlockAlertsDemo/BlockTextPromptAlertView.m new file mode 100644 index 0000000..44eeec9 --- /dev/null +++ b/BlockAlertsDemo/BlockTextPromptAlertView.m @@ -0,0 +1,121 @@ +// +// BlockTextPromptAlertView.m +// BlockAlertsDemo +// +// Created by Barrett Jacobsen on 2/13/12. +// Copyright (c) 2012 Barrett Jacobsen. All rights reserved. +// + +#import "BlockTextPromptAlertView.h" + +#define kTextBoxHeight 31 +#define kTextBoxSpacing 5 +#define kTextBoxHorizontalMargin 12 + +#define kKeyboardResizeBounce 20 + + +@implementation BlockTextPromptAlertView +@synthesize textField; +@synthesize enteredText; + ++ (BlockTextPromptAlertView *)promptWithTitle:(NSString *)title message:(NSString *)message defaultText:(NSString*)defaultText { + + return [[[BlockTextPromptAlertView alloc] initWithTitle:title message:message defaultText:defaultText] autorelease]; + +} + +- (id)initWithTitle:(NSString *)title message:(NSString *)message defaultText:(NSString*)defaultText { + + self = [super initWithTitle:title message:message]; + + if (self) { + UITextField *theTextField = [[UITextField alloc] initWithFrame:CGRectMake(kTextBoxHorizontalMargin, _height, _view.bounds.size.width - kTextBoxHorizontalMargin * 2, kTextBoxHeight)]; + + [theTextField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter]; + [theTextField setAutocapitalizationType:UITextAutocapitalizationTypeWords]; + [theTextField setBorderStyle:UITextBorderStyleRoundedRect]; + [theTextField setTextAlignment:UITextAlignmentCenter]; + [theTextField setClearButtonMode:UITextFieldViewModeAlways]; + + if (defaultText) + theTextField.text = defaultText; + + [_view addSubview:theTextField]; + + self.textField = theTextField; + + _height += kTextBoxHeight + kTextBoxSpacing; + } + + return self; +} + +- (void)show { + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(keyboardWillShow:) + name:UIKeyboardWillShowNotification + object:nil]; + + [super show]; + + [self.textField performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:0.5]; +} + +- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated { + [super dismissWithClickedButtonIndex:buttonIndex animated:animated]; + + [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; +} + +- (void)keyboardWillShow:(NSNotification *)notification { + CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; + + CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height; + __block CGRect frame = _view.frame; + + if (frame.origin.y + frame.size.height > screenHeight - keyboardSize.height) { + + frame.origin.y = screenHeight - keyboardSize.height - frame.size.height; + + if (frame.origin.y < 0) + frame.origin.y = 0; + + [UIView animateWithDuration:0.3 + delay:0.0 + options:UIViewAnimationCurveEaseOut + animations:^{ + _view.frame = frame; + } + completion:nil]; + } +} + + +- (void)setAllowableCharacters:(NSString*)accepted { + unacceptedInput = [[NSCharacterSet characterSetWithCharactersInString:accepted] invertedSet]; + self.textField.delegate = self; +} + +- (void)setMaxLength:(NSInteger)max { + maxLength = max; + self.textField.delegate = self; +} + +- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { + + NSUInteger newLength = [self.textField.text length] + [string length] - range.length; + + if (maxLength > 0 && newLength > maxLength) + return NO; + + if (!unacceptedInput) + return YES; + + if ([[string componentsSeparatedByCharactersInSet:unacceptedInput] count] > 1) + return NO; + else + return YES; +} + +@end