Adding support to customize the textfield with object #33

Closed
wants to merge 2 commits into from
View
6 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 = "<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; };
@@ -240,6 +243,8 @@
ECFC5EFB14EB1DA4004BFD45 /* BlockBackground.m */,
ECFC5EFC14EB1DA4004BFD45 /* BlockTextPromptAlertView.h */,
ECFC5EFD14EB1DA4004BFD45 /* BlockTextPromptAlertView.m */,
+ 2008CDE115E5743100215B67 /* BlockTextAlertViewConfig.h */,
+ 2008CDE215E5743100215B67 /* BlockTextAlertViewConfig.m */,
);
path = ToAddToYourProjects;
sourceTree = "<group>";
@@ -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;
};
View
6 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];
View
10 BlockAlertsDemo/ToAddToYourProjects/BlockAlertView.h
@@ -5,9 +5,17 @@
#import <UIKit/UIKit.h>
+typedef void(^BlockAlert)();
+
+typedef enum {
+ BlockAlertViewButtonClorsBlack = 0,
+ BlockAlertViewButtonClorsGray,
+ BlockAlertViewButtonClorsRed,
+} BlockAlertViewButtonClors;
+
@interface BlockAlertView : NSObject {
@protected
- UIView *_view;
+ UIScrollView *_view;
NSMutableArray *_blocks;
CGFloat _height;
}
View
121 BlockAlertsDemo/ToAddToYourProjects/BlockAlertView.m
@@ -7,6 +7,39 @@
#import "BlockBackground.h"
#import "BlockUI.h"
+@interface BlockAlertObject : NSObject
+@property(nonatomic) BlockAlertViewButtonClors color;
+@property(nonatomic, retain) NSString *title;
+@property(nonatomic, copy) BlockAlert block;
+
++ (BlockAlertObject *) blockObjectWithBlock: (BlockAlert) block
+ color: (BlockAlertViewButtonClors) color
+ andTitle: (NSString *) title;
+
+@end
+
+@implementation BlockAlertObject
+@synthesize color = _color, title = _title, block = _block;
+
++ (BlockAlertObject *) blockObjectWithBlock: (BlockAlert) block
+ color: (BlockAlertViewButtonClors) color
+ andTitle: (NSString *) title {
+ BlockAlertObject *blockObject = [[[self alloc] init] autorelease];
+ blockObject.block = block;
+ blockObject.color = color;
+ blockObject.title = title;
+ return blockObject;
+}
+- (void)setBlock:(BlockAlert)block{
+ _block = block ? [[block copy] autorelease] : [NSNull null];
+ [_block retain];
+}
+- (void)dealloc{
+ [[self block] release];
+ [super dealloc];
+}
+@end
+
@implementation BlockAlertView
@synthesize view = _view;
@@ -49,7 +82,7 @@ - (id)initWithTitle:(NSString *)title message:(NSString *)message
frame.origin.x = floorf((frame.size.width - background.size.width) * 0.5);
frame.size.width = background.size.width;
- _view = [[UIView alloc] initWithFrame:frame];
+ _view = [[UIScrollView alloc] initWithFrame:frame];
_blocks = [[NSMutableArray alloc] init];
_height = kAlertViewBorder + 6;
@@ -102,7 +135,25 @@ - (id)initWithTitle:(NSString *)title message:(NSString *)message
return self;
}
-
+- (NSString*)formatColorTypeToString:(BlockAlertViewButtonClors) color{
+ NSString *result = nil;
+
+ switch(color) {
+ case BlockAlertViewButtonClorsBlack:
+ result = @"black";
+ break;
+ case BlockAlertViewButtonClorsGray:
+ result = @"gray";
+ break;
+ case BlockAlertViewButtonClorsRed:
+ result = @"red";
+ break;
+ default:
+ [NSException raise:NSGenericException format:@"Unexpected FormatType."];
+ }
+
+ return result;
+}
- (void)dealloc
{
[_backgroundImage release];
@@ -114,40 +165,38 @@ - (void)dealloc
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - Public
-- (void)addButtonWithTitle:(NSString *)title color:(NSString*)color block:(void (^)())block
+- (void)addButtonWithTitle:(NSString *)title
+ color:(BlockAlertViewButtonClors) color
+ block:(BlockAlert)block
{
- [_blocks addObject:[NSArray arrayWithObjects:
- block ? [[block copy] autorelease] : [NSNull null],
- title,
- color,
- nil]];
+ BlockAlertObject *blockObject = [BlockAlertObject blockObjectWithBlock: block
+ color: color
+ andTitle: title];
+ [_blocks addObject:blockObject];
}
-- (void)addButtonWithTitle:(NSString *)title block:(void (^)())block
+- (void)addButtonWithTitle:(NSString *)title block:(BlockAlert)block
{
- [self addButtonWithTitle:title color:@"gray" block:block];
+ [self addButtonWithTitle:title color:BlockAlertViewButtonClorsGray block:block];
}
-- (void)setCancelButtonWithTitle:(NSString *)title block:(void (^)())block
+- (void)setCancelButtonWithTitle:(NSString *)title block:(BlockAlert)block
{
- [self addButtonWithTitle:title color:@"black" block:block];
+ [self addButtonWithTitle:title color:BlockAlertViewButtonClorsBlack block:block];
}
-- (void)setDestructiveButtonWithTitle:(NSString *)title block:(void (^)())block
+- (void)setDestructiveButtonWithTitle:(NSString *)title block:(BlockAlert)block
{
- [self addButtonWithTitle:title color:@"red" block:block];
+ [self addButtonWithTitle:title color:BlockAlertViewButtonClorsRed block:block];
}
- (void)show
{
BOOL isSecondButton = NO;
NSUInteger index = 0;
- for (NSUInteger i = 0; i < _blocks.count; i++)
- {
- NSArray *block = [_blocks objectAtIndex:i];
- NSString *title = [block objectAtIndex:1];
- NSString *color = [block objectAtIndex:2];
-
+ for (BlockAlertObject *blockObject in _blocks) {
+ NSInteger i = [_blocks indexOfObject:blockObject];
+ NSString *color = [self formatColorTypeToString:blockObject.color];
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"alert-%@-button.png", color]];
image = [image stretchableImageWithLeftCapWidth:(int)(image.size.width+1)>>1 topCapHeight:0];
@@ -164,25 +213,23 @@ - (void)show
{
// In this case there's another button.
// Let's check if they fit on the same line.
- CGSize size = [title sizeWithFont:buttonFont
+ CGSize size = [blockObject.title sizeWithFont:buttonFont
minFontSize:10
actualFontSize:nil
forWidth:_view.bounds.size.width-kAlertViewBorder*2
lineBreakMode:UILineBreakModeClip];
- if (size.width < maxHalfWidth - kAlertViewBorder)
- {
+ if (size.width < maxHalfWidth - kAlertViewBorder){
// It might fit. Check the next Button
- NSArray *block2 = [_blocks objectAtIndex:i+1];
- NSString *title2 = [block2 objectAtIndex:1];
- size = [title2 sizeWithFont:buttonFont
+ BlockAlertObject *blockObject2 = [_blocks objectAtIndex:i+1];
+ NSString *title2 = blockObject2.title;
+ size = [title2 sizeWithFont:buttonFont
minFontSize:10
actualFontSize:nil
forWidth:_view.bounds.size.width-kAlertViewBorder*2
lineBreakMode:UILineBreakModeClip];
-
- if (size.width < maxHalfWidth - kAlertViewBorder)
- {
+
+ if (size.width < maxHalfWidth - kAlertViewBorder){
// They'll fit!
isSecondButton = YES; // For the next iteration
width = maxHalfWidth;
@@ -192,7 +239,7 @@ - (void)show
else if (_blocks.count == 1)
{
// In this case this is the ony button. We'll size according to the text
- CGSize size = [title sizeWithFont:buttonFont
+ CGSize size = [blockObject.title sizeWithFont:buttonFont
minFontSize:10
actualFontSize:nil
forWidth:_view.bounds.size.width-kAlertViewBorder*2
@@ -218,8 +265,8 @@ - (void)show
[button setBackgroundImage:image forState:UIControlStateNormal];
[button setTitleColor:kAlertViewButtonTextColor forState:UIControlStateNormal];
[button setTitleShadowColor:kAlertViewButtonShadowColor forState:UIControlStateNormal];
- [button setTitle:title forState:UIControlStateNormal];
- button.accessibilityLabel = title;
+ [button setTitle:blockObject.title forState:UIControlStateNormal];
+ button.accessibilityLabel = blockObject.title;
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
@@ -297,11 +344,11 @@ - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)anim
{
if (buttonIndex >= 0 && buttonIndex < [_blocks count])
{
- id obj = [[_blocks objectAtIndex: buttonIndex] objectAtIndex:0];
- if (![obj isEqual:[NSNull null]])
- {
- ((void (^)())obj)();
- }
+ BlockAlertObject *blockObject = [_blocks objectAtIndex: buttonIndex];
+ BlockAlert block = blockObject.block;
+ if (![block isEqual:[NSNull null]]){
+ block();
+ }
}
if (animated)
View
23 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
View
24 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
View
8 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;
View
34 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;