Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 9d9739d2f6
Fetching contributors…

Cannot retrieve contributors at this time

172 lines (124 sloc) 6.049 kb
//
// BlockTextPromptAlertView.m
// BlockAlertsDemo
//
// Created by Barrett Jacobsen on 2/13/12.
// Copyright (c) 2012 Barrett Jacobsen. All rights reserved.
//
#import "BlockTextPromptAlertView.h"
#import "BlockTextAlertViewConfig.h"
#define kTextBoxHeight 31
#define kTextBoxSpacing 5
#define kTextBoxHorizontalMargin 12
#define kKeyboardResizeBounce 20
@interface BlockTextPromptAlertView()
@property(nonatomic, copy) TextFieldReturnCallBack callBack;
@end
@implementation BlockTextPromptAlertView
@synthesize textField, callBack;
@synthesize config = _config;
+ (BlockTextPromptAlertView *)promptWithTitle:(NSString *)title message:(NSString *)message defaultText:(NSString*)defaultText {
return [self promptWithTitle:title message:message defaultText:defaultText block:nil];
}
+ (BlockTextPromptAlertView *)promptWithTitle:(NSString *)title message:(NSString *)message defaultText:(NSString*)defaultText block:(TextFieldReturnCallBack)block {
return [[[BlockTextPromptAlertView alloc] initWithTitle:title message:message defaultText:defaultText block:block forConfig:nil] autorelease];
}
+ (BlockTextPromptAlertView *)promptWithTitle:(NSString *)title message:(NSString *)message textField:(out UITextField**)textField {
return [self promptWithTitle:title message:message textField:textField block:nil forConfig:nil];
}
+ (BlockTextPromptAlertView *)promptWithTitle:(NSString *)title message:(NSString *)message textField:(out UITextField**)textField block:(TextFieldReturnCallBack) block forConfig:(BlockTextAlertViewConfig *)config{
BlockTextPromptAlertView *prompt = [[[BlockTextPromptAlertView alloc] initWithTitle:title message:message defaultText:nil block:block forConfig:config] autorelease];
*textField = prompt.textField;
return prompt;
}
- (id)initWithTitle:(NSString *)title message:(NSString *)message defaultText:(NSString*)defaultText block: (TextFieldReturnCallBack) block forConfig: (BlockTextAlertViewConfig *) config {
self = [super initWithTitle:title message:message];
if (self) {
_config = config;
UITextField *theTextField = [[[UITextField alloc] initWithFrame:CGRectMake(kTextBoxHorizontalMargin, _height, _view.bounds.size.width - kTextBoxHorizontalMargin * 2, kTextBoxHeight)] autorelease];
[theTextField setContentVerticalAlignment:[self.config contentVerticalAlignment]];
[theTextField setAutocapitalizationType:[self.config autocapitalizationType]];
[theTextField setBorderStyle:[self.config borderStyle]];
[theTextField setTextAlignment:[self.config textAlignment]];
[theTextField setClearButtonMode:[self.config clearButtonMode]];
[theTextField setFont:[self.config font]];
[theTextField setPlaceholder:[self.config placeholder]];
if (defaultText)
theTextField.text = defaultText;
if(block){
theTextField.delegate = self;
}
[_view addSubview:theTextField];
self.textField = theTextField;
_height += kTextBoxHeight + kTextBoxSpacing;
self.callBack = block;
}
return self;
}
- (void)show {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[super show];
[[NSNotificationCenter defaultCenter] addObserver:textField selector:@selector(becomeFirstResponder) name:@"AlertViewFinishedAnimations" object:nil];
}
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated {
[super dismissWithClickedButtonIndex:buttonIndex animated:animated];
[[NSNotificationCenter defaultCenter] removeObserver:textField];
[[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)textFieldShouldReturn:(UITextField *)_textField{
if(callBack){
return callBack(self);
}
return NO;
}
- (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;
}
-(BlockTextAlertViewConfig *)config {
if(!_config){
_config = [BlockTextAlertViewConfig defaultConfig];
}
return _config;
}
- (void)dealloc
{
self.callBack = nil;
[super dealloc];
}
@end
Jump to Line
Something went wrong with that request. Please try again.