From 5e3d48174da00681635c3d9e2593deac01107f54 Mon Sep 17 00:00:00 2001 From: Diego Chohfi Date: Wed, 22 Aug 2012 17:14:50 -0300 Subject: [PATCH] Adding support to customize the textfield with object --- BlockAlertsDemo.xcodeproj/project.pbxproj | 6 ++++ .../BlockAlertsDemoViewController.m | 6 +++- .../BlockTextAlertViewConfig.h | 23 +++++++++++++ .../BlockTextAlertViewConfig.m | 24 +++++++++++++ .../BlockTextPromptAlertView.h | 8 +++-- .../BlockTextPromptAlertView.m | 34 +++++++++++++------ 6 files changed, 86 insertions(+), 15 deletions(-) create mode 100644 BlockAlertsDemo/ToAddToYourProjects/BlockTextAlertViewConfig.h create mode 100644 BlockAlertsDemo/ToAddToYourProjects/BlockTextAlertViewConfig.m diff --git a/BlockAlertsDemo.xcodeproj/project.pbxproj b/BlockAlertsDemo.xcodeproj/project.pbxproj index 9d0b3ad..c895b28 100644 --- a/BlockAlertsDemo.xcodeproj/project.pbxproj +++ b/BlockAlertsDemo.xcodeproj/project.pbxproj @@ -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 */; }; @@ -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 = ""; }; + 2008CDE215E5743100215B67 /* BlockTextAlertViewConfig.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BlockTextAlertViewConfig.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; }; @@ -240,6 +243,8 @@ ECFC5EFB14EB1DA4004BFD45 /* BlockBackground.m */, ECFC5EFC14EB1DA4004BFD45 /* BlockTextPromptAlertView.h */, ECFC5EFD14EB1DA4004BFD45 /* BlockTextPromptAlertView.m */, + 2008CDE115E5743100215B67 /* BlockTextAlertViewConfig.h */, + 2008CDE215E5743100215B67 /* BlockTextAlertViewConfig.m */, ); path = ToAddToYourProjects; sourceTree = ""; @@ -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; }; diff --git a/BlockAlertsDemo/BlockAlertsDemoViewController.m b/BlockAlertsDemo/BlockAlertsDemoViewController.m index 6a758f7..3acc3ee 100644 --- a/BlockAlertsDemo/BlockAlertsDemoViewController.m +++ b/BlockAlertsDemo/BlockAlertsDemoViewController.m @@ -10,6 +10,7 @@ #import "BlockAlertView.h" #import "BlockActionSheet.h" #import "BlockTextPromptAlertView.h" +#import "BlockTextAlertViewConfig.h" @implementation BlockAlertsDemoViewController @synthesize testKeyboard; @@ -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]; diff --git a/BlockAlertsDemo/ToAddToYourProjects/BlockTextAlertViewConfig.h b/BlockAlertsDemo/ToAddToYourProjects/BlockTextAlertViewConfig.h new file mode 100644 index 0000000..3f2babf --- /dev/null +++ b/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 + +@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 diff --git a/BlockAlertsDemo/ToAddToYourProjects/BlockTextAlertViewConfig.m b/BlockAlertsDemo/ToAddToYourProjects/BlockTextAlertViewConfig.m new file mode 100644 index 0000000..95b2a68 --- /dev/null +++ b/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 diff --git a/BlockAlertsDemo/ToAddToYourProjects/BlockTextPromptAlertView.h b/BlockAlertsDemo/ToAddToYourProjects/BlockTextPromptAlertView.h index 7405e3b..303fda3 100644 --- a/BlockAlertsDemo/ToAddToYourProjects/BlockTextPromptAlertView.h +++ b/BlockAlertsDemo/ToAddToYourProjects/BlockTextPromptAlertView.h @@ -8,7 +8,7 @@ #import "BlockAlertView.h" -@class BlockTextPromptAlertView; +@class BlockTextPromptAlertView, BlockTextAlertViewConfig; typedef BOOL(^TextFieldReturnCallBack)(BlockTextPromptAlertView *); @@ -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; diff --git a/BlockAlertsDemo/ToAddToYourProjects/BlockTextPromptAlertView.m b/BlockAlertsDemo/ToAddToYourProjects/BlockTextPromptAlertView.m index e5de5e6..68af3b1 100644 --- a/BlockAlertsDemo/ToAddToYourProjects/BlockTextPromptAlertView.m +++ b/BlockAlertsDemo/ToAddToYourProjects/BlockTextPromptAlertView.m @@ -7,7 +7,7 @@ // #import "BlockTextPromptAlertView.h" - +#import "BlockTextAlertViewConfig.h" #define kTextBoxHeight 31 #define kTextBoxSpacing 5 #define kTextBoxHorizontalMargin 12 @@ -20,6 +20,7 @@ @interface BlockTextPromptAlertView() @implementation BlockTextPromptAlertView @synthesize textField, callBack; +@synthesize config = _config; @@ -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; @@ -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;