Skip to content

Commit

Permalink
Adding support to customize the textfield with object
Browse files Browse the repository at this point in the history
  • Loading branch information
dchohfi committed Aug 22, 2012
1 parent e3276bf commit 5e3d481
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 15 deletions.
6 changes: 6 additions & 0 deletions BlockAlertsDemo.xcodeproj/project.pbxproj
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
2008CDE315E5743100215B67 /* BlockTextAlertViewConfig.m in Sources */ = {isa = PBXBuildFile; fileRef = 2008CDE215E5743100215B67 /* BlockTextAlertViewConfig.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 @@ -50,6 +51,8 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
2008CDE115E5743100215B67 /* BlockTextAlertViewConfig.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BlockTextAlertViewConfig.h; sourceTree = "<group>"; };
2008CDE215E5743100215B67 /* BlockTextAlertViewConfig.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BlockTextAlertViewConfig.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 @@ -240,6 +243,8 @@
ECFC5EFB14EB1DA4004BFD45 /* BlockBackground.m */,
ECFC5EFC14EB1DA4004BFD45 /* BlockTextPromptAlertView.h */,
ECFC5EFD14EB1DA4004BFD45 /* BlockTextPromptAlertView.m */,
2008CDE115E5743100215B67 /* BlockTextAlertViewConfig.h */,
2008CDE215E5743100215B67 /* BlockTextAlertViewConfig.m */,
);
path = ToAddToYourProjects;
sourceTree = "<group>";
Expand Down Expand Up @@ -342,6 +347,7 @@
ECFC5EFF14EB1DA4004BFD45 /* BlockAlertView.m in Sources */,
ECFC5F0014EB1DA4004BFD45 /* BlockBackground.m in Sources */,
ECFC5F0114EB1DA4004BFD45 /* BlockTextPromptAlertView.m in Sources */,
2008CDE315E5743100215B67 /* BlockTextAlertViewConfig.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
6 changes: 5 additions & 1 deletion BlockAlertsDemo/BlockAlertsDemoViewController.m
Expand Up @@ -10,6 +10,7 @@
#import "BlockAlertView.h"
#import "BlockActionSheet.h"
#import "BlockTextPromptAlertView.h"
#import "BlockTextAlertViewConfig.h"

@implementation BlockAlertsDemoViewController
@synthesize testKeyboard;
Expand Down Expand Up @@ -85,11 +86,14 @@ - (IBAction)goNuts:(id)sender

- (IBAction)showTextPrompt:(id)sender
{
BlockTextAlertViewConfig *config = [BlockTextAlertViewConfig defaultConfig];
[config setPlaceholder:@"Placeholder"];

UITextField *textField;
BlockTextPromptAlertView *alert = [BlockTextPromptAlertView promptWithTitle:@"Prompt Title" message:@"With prompts you do have to keep in mind limited screen space due to the keyboard" textField:&textField block:^(BlockTextPromptAlertView *alert){
[alert.textField resignFirstResponder];
return YES;
}];
} forConfig:config];


[alert setCancelButtonWithTitle:@"Cancel" block:nil];
Expand Down
23 changes: 23 additions & 0 deletions BlockAlertsDemo/ToAddToYourProjects/BlockTextAlertViewConfig.h
@@ -0,0 +1,23 @@
//
// BlockTextAlertViewConfig.h
// BlockAlertsDemo
//
// Created by Diego Chohfi on 8/22/12.
// Copyright (c) 2012 CodeCrop Software. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface BlockTextAlertViewConfig : NSObject

@property(nonatomic) UIControlContentVerticalAlignment contentVerticalAlignment;
@property(nonatomic) UITextAutocapitalizationType autocapitalizationType;
@property(nonatomic) UITextBorderStyle borderStyle;
@property(nonatomic) UITextAlignment textAlignment;
@property(nonatomic) UITextFieldViewMode clearButtonMode;
@property(nonatomic,copy) NSString *placeholder;
@property(nonatomic,retain) UIFont *font;

+ (BlockTextAlertViewConfig *) defaultConfig;

@end
24 changes: 24 additions & 0 deletions BlockAlertsDemo/ToAddToYourProjects/BlockTextAlertViewConfig.m
@@ -0,0 +1,24 @@
//
// BlockTextAlertViewConfig.m
// BlockAlertsDemo
//
// Created by Diego Chohfi on 8/22/12.
// Copyright (c) 2012 CodeCrop Software. All rights reserved.
//

#import "BlockTextAlertViewConfig.h"

@implementation BlockTextAlertViewConfig

+ (BlockTextAlertViewConfig *) defaultConfig {
BlockTextAlertViewConfig *config = [[[self alloc] init] autorelease];
[config setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[config setAutocapitalizationType:UITextAutocapitalizationTypeWords];
[config setBorderStyle:UITextBorderStyleRoundedRect];
[config setTextAlignment:UITextAlignmentCenter];
[config setClearButtonMode:UITextFieldViewModeAlways];
[config setPlaceholder:nil];
[config setFont:nil];
return config;
}
@end
Expand Up @@ -8,7 +8,7 @@

#import "BlockAlertView.h"

@class BlockTextPromptAlertView;
@class BlockTextPromptAlertView, BlockTextAlertViewConfig;

typedef BOOL(^TextFieldReturnCallBack)(BlockTextPromptAlertView *);

Expand All @@ -19,16 +19,18 @@ typedef BOOL(^TextFieldReturnCallBack)(BlockTextPromptAlertView *);
}

@property (nonatomic, retain) UITextField *textField;
@property (nonatomic, readonly) BlockTextAlertViewConfig *config;

+ (BlockTextPromptAlertView *)promptWithTitle:(NSString *)title message:(NSString *)message defaultText:(NSString*)defaultText;

+ (BlockTextPromptAlertView *)promptWithTitle:(NSString *)title message:(NSString *)message defaultText:(NSString*)defaultText block:(TextFieldReturnCallBack) block;

+ (BlockTextPromptAlertView *)promptWithTitle:(NSString *)title message:(NSString *)message textField:(out UITextField**)textField;

+ (BlockTextPromptAlertView *)promptWithTitle:(NSString *)title message:(NSString *)message textField:(out UITextField**)textField block:(TextFieldReturnCallBack) block;
+ (BlockTextPromptAlertView *)promptWithTitle:(NSString *)title message:(NSString *)message textField:(out UITextField**)textField block:(TextFieldReturnCallBack) block forConfig: (BlockTextAlertViewConfig *) config;


- (id)initWithTitle:(NSString *)title message:(NSString *)message defaultText:(NSString*)defaultText block: (TextFieldReturnCallBack) block;
- (id)initWithTitle:(NSString *)title message:(NSString *)message defaultText:(NSString*)defaultText block: (TextFieldReturnCallBack) block forConfig: (BlockTextAlertViewConfig *) config;


- (void)setAllowableCharacters:(NSString*)accepted;
Expand Down
34 changes: 23 additions & 11 deletions BlockAlertsDemo/ToAddToYourProjects/BlockTextPromptAlertView.m
Expand Up @@ -7,7 +7,7 @@
//

#import "BlockTextPromptAlertView.h"

#import "BlockTextAlertViewConfig.h"
#define kTextBoxHeight 31
#define kTextBoxSpacing 5
#define kTextBoxHorizontalMargin 12
Expand All @@ -20,6 +20,7 @@ @interface BlockTextPromptAlertView()

@implementation BlockTextPromptAlertView
@synthesize textField, callBack;
@synthesize config = _config;



Expand All @@ -28,34 +29,38 @@ + (BlockTextPromptAlertView *)promptWithTitle:(NSString *)title message:(NSStrin
}

+ (BlockTextPromptAlertView *)promptWithTitle:(NSString *)title message:(NSString *)message defaultText:(NSString*)defaultText block:(TextFieldReturnCallBack)block {
return [[[BlockTextPromptAlertView alloc] initWithTitle:title message:message defaultText:defaultText block:block] autorelease];
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];
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{
BlockTextPromptAlertView *prompt = [[[BlockTextPromptAlertView alloc] initWithTitle:title message:message defaultText:nil block:block] autorelease];
+ (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 {
- (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:UIControlContentVerticalAlignmentCenter];
[theTextField setAutocapitalizationType:UITextAutocapitalizationTypeWords];
[theTextField setBorderStyle:UITextBorderStyleRoundedRect];
[theTextField setTextAlignment:UITextAlignmentCenter];
[theTextField setClearButtonMode:UITextFieldViewModeAlways];
[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;
Expand Down Expand Up @@ -150,6 +155,13 @@ - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRang
return YES;
}

-(BlockTextAlertViewConfig *)config {
if(!_config){
_config = [BlockTextAlertViewConfig defaultConfig];
}
return _config;
}

- (void)dealloc
{
self.callBack = nil;
Expand Down

0 comments on commit 5e3d481

Please sign in to comment.