Skip to content

Commit

Permalink
Added BlockTextPromptAlertView
Browse files Browse the repository at this point in the history
  • Loading branch information
barrettj committed Feb 13, 2012
1 parent b3d9c04 commit 6fdaba0
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 1 deletion.
6 changes: 6 additions & 0 deletions BlockAlertsDemo.xcodeproj/project.pbxproj
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -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 = "<group>"; };
B5B0620B14E95174007F2C78 /* BlockTextPromptAlertView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BlockTextPromptAlertView.m; sourceTree = "<group>"; };
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; };
Expand Down Expand Up @@ -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 = "<group>";
Expand Down Expand Up @@ -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;
};
Expand Down
2 changes: 1 addition & 1 deletion BlockAlertsDemo/BlockAlertView.h
Expand Up @@ -6,7 +6,7 @@
#import <UIKit/UIKit.h>

@interface BlockAlertView : NSObject {
@private
@protected
UIView *_view;
NSMutableArray *_blocks;
CGFloat _height;
Expand Down
30 changes: 30 additions & 0 deletions 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 <UITextFieldDelegate> {

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
121 changes: 121 additions & 0 deletions 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

0 comments on commit 6fdaba0

Please sign in to comment.