Skip to content

Commit

Permalink
Integrated JVFloatLabeledTextField, available via BPFormFloatLabelInp…
Browse files Browse the repository at this point in the history
…utCell. Old behavior preserved, backwards compatible.
  • Loading branch information
bpoplauschi committed Mar 13, 2014
1 parent 45d98a5 commit 00d45a7
Show file tree
Hide file tree
Showing 17 changed files with 321 additions and 46 deletions.
1 change: 1 addition & 0 deletions BPForms/BPAppearance.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

// -- Fonts --
@property (nonatomic, strong) UIFont *inputCellTextFieldFont; // default is system-14
@property (nonatomic, strong) UIFont *inputCellTextFieldFloatingLabelFont; // default is system-8

@property (nonatomic, strong) UIFont *infoCellLabelFont; // default is system-12

Expand Down
1 change: 1 addition & 0 deletions BPForms/BPAppearance.m
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ - (void)loadDefaults {
self.headerFooterLabelTextColor = [UIColor darkGrayColor];

self.inputCellTextFieldFont = [UIFont systemFontOfSize:14.0f];
self.inputCellTextFieldFloatingLabelFont= [UIFont systemFontOfSize: 8.0f];
self.infoCellLabelFont = [UIFont systemFontOfSize:12.0f];
self.headerFooterLabelFont = [UIFont systemFontOfSize:12.0f];

Expand Down
9 changes: 0 additions & 9 deletions BPForms/BPFormTextField.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,9 @@
// SOFTWARE.


@class BPFormInputCell;

/**
* Subclass UITextField to adjust the layout (i.e. start text with a little offset)
*/
@interface BPFormTextField : UITextField

/**
* Retrieve the input cell containing the current text field
*
* @return the instance of the BPFormInputCell or nil
*/
- (BPFormInputCell *)containerTableViewCell;

@end
31 changes: 6 additions & 25 deletions BPForms/BPFormTextField.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,19 @@

#import "BPFormTextField.h"
#import "BPFormInputCell.h"
#import "UITextField+BPForms.h"

@implementation BPFormTextField

/**
* The following methods add an x offset to the textfield text
*/
- (CGRect)textRectForBounds:(CGRect)inBounds {
return [self _adjustRectForBounds:inBounds];
return [self addXOffset:5 toBounds:inBounds];
}

- (CGRect)editingRectForBounds:(CGRect)inBounds {
return [self _adjustRectForBounds:inBounds];
}

- (CGRect)_adjustRectForBounds:(CGRect)inBounds {
CGRect frame = inBounds;
// add a 5 pixel origin.x offset so that the text doesn't start right from the margin
frame.origin.x = 5;
frame.size.width -=5;
// also, on iOS6 and earlier, the text starts right from the top, so add a similar 12 pixel vertical offset
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0) {
frame.origin.y = 12;
}
inBounds = frame;
return CGRectInset(inBounds, 0, 0);
}

- (BPFormInputCell *)containerTableViewCell {
UIView *view = self;
while ((view = view.superview)) {
if ([view isKindOfClass:[BPFormCell class]]) {
break;
}
}
return (BPFormInputCell *)view;
return [self addXOffset:5 toBounds:inBounds];
}

@end
9 changes: 5 additions & 4 deletions BPForms/BPFormViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#import "BPFormTextField.h"
#import "BPFormInfoCell.h"
#import <Masonry.h>
#import "UITextField+BPForms.h"


@interface BPFormViewController ()
Expand Down Expand Up @@ -214,7 +215,7 @@ - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSIntege
- (void)textFieldDidBeginEditing:(UITextField *)textField {
BPFormInputCell *cell = nil;
if ([textField isKindOfClass:[BPFormTextField class]]) {
cell = [((BPFormTextField *)textField) containerTableViewCell];
cell = [textField containerInputCell];
}
if (!cell) {
return;
Expand All @@ -232,7 +233,7 @@ - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRang
BOOL shouldChange = YES;
BPFormInputCell *cell = nil;
if ([textField isKindOfClass:[BPFormTextField class]]) {
cell = [((BPFormTextField *)textField) containerTableViewCell];
cell = [textField containerInputCell];
}
if (!cell) {
return YES;
Expand All @@ -251,7 +252,7 @@ - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRang
- (void)textFieldDidEndEditing:(UITextField *)textField {
BPFormInputCell *cell = nil;
if ([textField isKindOfClass:[BPFormTextField class]]) {
cell = [((BPFormTextField *)textField) containerTableViewCell];
cell = [textField containerInputCell];
}
if (!cell) {
return;
Expand All @@ -274,7 +275,7 @@ - (BOOL)textFieldShouldReturn:(UITextField *)textField {
BOOL shouldReturn = YES;
BPFormInputCell *cell = nil;
if ([textField isKindOfClass:[BPFormTextField class]]) {
cell = [((BPFormTextField *)textField) containerTableViewCell];
cell = [textField containerInputCell];
}
if (!cell) {
return YES;
Expand Down
11 changes: 8 additions & 3 deletions BPForms/Cells/BPFormInputCell.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ typedef NS_ENUM(NSInteger, BPFormValidationState) {
@interface BPFormInputCell : BPFormCell

// UI components
@property (nonatomic, strong) BPFormTextField *textField;
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UIImageView *mandatoryImageView;
@property (nonatomic, strong) UIImageView *validationImageView;

Expand All @@ -57,6 +57,11 @@ typedef NS_ENUM(NSInteger, BPFormValidationState) {
@property (nonatomic, copy) TextFieldShouldEditBlock shouldChangeTextBlock; // Block called from `textfield:shouldChangeCharactersInRange:replacementString:`. Return YES if the text should change
@property (nonatomic, copy) TextFieldShouldEditBlock shouldReturnBlock; // Block called from `textFieldShouldReturn:`. Return YES if the text should change

/**
* By default BPFormInputCell uses the BPFormTextField class for text fields. This can be customized by providing a different class
*/
+ (Class)textFieldClass;

/**
* Will update the UI according to the validation state
*/
Expand All @@ -67,14 +72,14 @@ typedef NS_ENUM(NSInteger, BPFormValidationState) {
*
* @param inMandatoryImageName - the image name must point to a file in the main bundle
*/
+(void)setMandatoryImageName:(NSString *)inMandatoryImageName;
+ (void)setMandatoryImageName:(NSString *)inMandatoryImageName;

/**
* Use to set custom images as valid and invalid icons
*
* @param inValidImageName - the image name must point to a file in the main bundle
* @param inInvalidImageName - the image name must point to a file in the main bundle
*/
+(void)setValidImageName:(NSString *)inValidImageName invalidImageName:(NSString *)inInvalidImageName;
+ (void)setValidImageName:(NSString *)inValidImageName invalidImageName:(NSString *)inInvalidImageName;

@end
15 changes: 12 additions & 3 deletions BPForms/Cells/BPFormInputCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,15 @@ TextFieldShouldEditBlock BPTextFieldValidateBlockWithPatternAndMessage(NSString

@implementation BPFormInputCell

+(void)setMandatoryImageName:(NSString *)inMandatoryImageName {
+ (Class)textFieldClass {
return [BPFormTextField class];
}

+ (void)setMandatoryImageName:(NSString *)inMandatoryImageName {
BPMandatoryImageName = inMandatoryImageName;
}

+(void)setValidImageName:(NSString *)inValidImageName invalidImageName:(NSString *)inInvalidImageName {
+ (void)setValidImageName:(NSString *)inValidImageName invalidImageName:(NSString *)inInvalidImageName {
BPValidImageName = inValidImageName;
BPInvalidImageName = inInvalidImageName;
}
Expand All @@ -83,7 +87,12 @@ - (void)setupCell {
}

- (void)setupTextField {
self.textField = [[BPFormTextField alloc] init];
Class textFieldClass = [[self class] textFieldClass];
if (!textFieldClass) {
textFieldClass = [BPFormTextField class];
}

self.textField = [[textFieldClass alloc] init];

self.textField.autocorrectionType = UITextAutocorrectionTypeNo;
self.textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
Expand Down
50 changes: 50 additions & 0 deletions BPForms/UITextField+BPForms.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// UITextField+BPForms.h
//
// Copyright (c) 2014 Bogdan Poplauschi
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.


@class BPFormInputCell;

/**
* The purpose is to add common functionality to all our textfields
*/
@interface UITextField (BPForms)

/**
* Retrieve the input cell containing the current text field
*
* @return the instance of the BPFormInputCell or nil
*/
- (BPFormInputCell *)containerInputCell;

/**
* Adds an X offset to bounds. Used by `textRectForBounds` and `editingRectForBounds` methods.
* The purpose is to avoid text values starting right from the edge.
*
* @param xOffset x offset in pixels
* @param inBounds the input bounds
*
* @return the output bounds
*/
- (CGRect)addXOffset:(CGFloat)xOffset toBounds:(CGRect)inBounds;

@end
54 changes: 54 additions & 0 deletions BPForms/UITextField+BPForms.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// UITextField+BPForms.m
//
// Copyright (c) 2014 Bogdan Poplauschi
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#import "UITextField+BPForms.h"
#import "BPFormInputCell.h"


@implementation UITextField (BPForms)

- (BPFormInputCell *)containerInputCell {
UIView *view = self;
while ((view = view.superview)) {
if ([view isKindOfClass:[BPFormCell class]]) {
break;
}
}
return (BPFormInputCell *)view;
}

- (CGRect)addXOffset:(CGFloat)xOffset toBounds:(CGRect)inBounds {
CGRect frame = inBounds;
frame.origin.x = xOffset;
frame.size.width -= xOffset;

// also, on iOS6 and earlier, the text starts right from the top, so add a similar 12 pixel vertical offset
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0) {
frame.origin.y += 12;
}

inBounds = frame;
return CGRectInset(inBounds, 0, 0);
}

@end
31 changes: 31 additions & 0 deletions BPFormsFloatLabel/BPFormFloatLabelInputCell.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// BPFormFloatLabelInputCell.h
//
// Copyright (c) 2014 Bogdan Poplauschi
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#import "BPFormInputCell.h"

/**
* Input cell with floating labels. Uses `JVFloatLabeledTextField`
*/
@interface BPFormFloatLabelInputCell : BPFormInputCell

@end
47 changes: 47 additions & 0 deletions BPFormsFloatLabel/BPFormFloatLabelInputCell.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// BPFormFloatLabelInputCell.m
//
// Copyright (c) 2014 Bogdan Poplauschi
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#import "BPFormFloatLabelInputCell.h"
#import "BPFormFloatLabelTextField.h"
#import "BPAppearance.h"

@implementation BPFormFloatLabelInputCell

+ (Class)textFieldClass {
return [BPFormFloatLabelTextField class];
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
if ([self.textField isKindOfClass:[BPFormFloatLabelTextField class]]) {
BPFormFloatLabelTextField *floatLabelTextField = (BPFormFloatLabelTextField *)self.textField;

[floatLabelTextField floatingLabel].font = [BPAppearance sharedInstance].inputCellTextFieldFloatingLabelFont;
[floatLabelTextField floatingLabel].backgroundColor = [UIColor clearColor];
}
}
return self;
}

@end
Loading

0 comments on commit 00d45a7

Please sign in to comment.