123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- // FloatLabeledTextFieldCell.m
- // XLForm ( https://github.com/xmartlabs/XLForm )
- //
- // Copyright (c) 2015 Xmartlabs ( http://xmartlabs.com )
- //
- //
- // 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 "FloatLabeledTextFieldCell.h"
- #import "UIView+XLFormAdditions.h"
- #import "JVFloatLabeledTextField.h"
- #import "NSObject+XLFormAdditions.h"
- #import <JVFloatLabeledTextField/JVFloatLabeledTextField.h>
- NSString * const XLFormRowDescriptorTypeFloatLabeledTextField = @"XLFormRowDescriptorTypeFloatLabeledTextField";
- const static CGFloat kVMargin = 8.0f;
- const static CGFloat kFloatingLabelFontSize = 11.0f;
- @interface FloatLabeledTextFieldCell () <UITextFieldDelegate>
- @property (nonatomic) JVFloatLabeledTextField * floatLabeledTextField;
- @end
- @implementation FloatLabeledTextFieldCell
- @synthesize floatLabeledTextField =_floatLabeledTextField;
- +(void)load
- {
- [XLFormViewController.cellClassesForRowDescriptorTypes setObject:[FloatLabeledTextFieldCell class] forKey:XLFormRowDescriptorTypeFloatLabeledTextField];
- }
- -(JVFloatLabeledTextField *)floatLabeledTextField
- {
- if (_floatLabeledTextField) return _floatLabeledTextField;
-
- _floatLabeledTextField = [JVFloatLabeledTextField autolayoutView];
- _floatLabeledTextField.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
- _floatLabeledTextField.floatingLabel.font = [UIFont boldSystemFontOfSize:kFloatingLabelFontSize];
- _floatLabeledTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
- return _floatLabeledTextField;
- }
- #pragma mark - XLFormDescriptorCell
- -(void)configure
- {
- [super configure];
- [self setSelectionStyle:UITableViewCellSelectionStyleNone];
- [self.contentView addSubview:self.floatLabeledTextField];
- [self.floatLabeledTextField setDelegate:self];
- [self.contentView addConstraints:[self layoutConstraints]];
- }
- -(void)update
- {
- [super update];
-
- self.floatLabeledTextField.attributedPlaceholder =
- [[NSAttributedString alloc] initWithString:self.rowDescriptor.title
- attributes:@{NSForegroundColorAttributeName: [UIColor lightGrayColor]}];
-
- self.floatLabeledTextField.text = self.rowDescriptor.value ? [self.rowDescriptor.value displayText] : self.rowDescriptor.noValueDisplayText;
- [self.floatLabeledTextField setEnabled:!self.rowDescriptor.isDisabled];
-
- self.floatLabeledTextField.floatingLabelTextColor = [UIColor lightGrayColor];
-
- [self.floatLabeledTextField setAlpha:((self.rowDescriptor.isDisabled) ? .6 : 1)];
- }
- -(BOOL)formDescriptorCellCanBecomeFirstResponder
- {
- return !self.rowDescriptor.isDisabled;
- }
- -(BOOL)formDescriptorCellBecomeFirstResponder
- {
- return [self.floatLabeledTextField becomeFirstResponder];
- }
- #pragma mark - UITextFieldDelegate
- - (BOOL)textFieldShouldClear:(UITextField *)textField
- {
- return [self.formViewController textFieldShouldClear:textField];
- }
- - (BOOL)textFieldShouldReturn:(UITextField *)textField
- {
- return [self.formViewController textFieldShouldReturn:textField];
- }
- - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
- {
- return [self.formViewController textFieldShouldBeginEditing:textField];
- }
- - (BOOL)textFieldShouldEndEditing:(UITextField *)textField
- {
- return [self.formViewController textFieldShouldEndEditing:textField];
- }
- - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
- return [self.formViewController textField:textField shouldChangeCharactersInRange:range replacementString:string];
- }
- - (void)textFieldDidBeginEditing:(UITextField *)textField
- {
- [self.formViewController textFieldDidBeginEditing:textField];
- }
- - (void)textFieldDidEndEditing:(UITextField *)textField
- {
- [self textFieldDidChange:textField];
- [self.formViewController textFieldDidEndEditing:textField];
- }
- -(void)setReturnKeyType:(UIReturnKeyType)returnKeyType
- {
- self.floatLabeledTextField.returnKeyType = returnKeyType;
- }
- -(UIReturnKeyType)returnKeyType
- {
- return self.floatLabeledTextField.returnKeyType;
- }
- +(CGFloat)formDescriptorCellHeightForRowDescriptor:(XLFormRowDescriptor *)rowDescriptor {
- return 55;
- }
- -(NSArray *)layoutConstraints
- {
- NSMutableArray * result = [[NSMutableArray alloc] init];
- NSDictionary * views = @{@"floatLabeledTextField": self.floatLabeledTextField};
- NSDictionary *metrics = @{@"vMargin":@(kVMargin)};
-
- [result addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[floatLabeledTextField]-|"
- options:0
- metrics:metrics
- views:views]];
- [result addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(vMargin)-[floatLabeledTextField]-(vMargin)-|"
- options:0
- metrics:metrics
- views:views]];
- return result;
- }
- #pragma mark - Helpers
- - (void)textFieldDidChange:(UITextField *)textField
- {
- if (self.floatLabeledTextField == textField) {
- if ([self.floatLabeledTextField.text length] > 0) {
- self.rowDescriptor.value = self.floatLabeledTextField.text;
- } else {
- self.rowDescriptor.value = nil;
- }
- }
- }
- @end
|