FloatLabeledTextFieldCell.m 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // FloatLabeledTextFieldCell.m
  2. // XLForm ( https://github.com/xmartlabs/XLForm )
  3. //
  4. // Copyright (c) 2015 Xmartlabs ( http://xmartlabs.com )
  5. //
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. #import "FloatLabeledTextFieldCell.h"
  25. #import "UIView+XLFormAdditions.h"
  26. #import "JVFloatLabeledTextField.h"
  27. #import "NSObject+XLFormAdditions.h"
  28. #import <JVFloatLabeledTextField/JVFloatLabeledTextField.h>
  29. NSString * const XLFormRowDescriptorTypeFloatLabeledTextField = @"XLFormRowDescriptorTypeFloatLabeledTextField";
  30. const static CGFloat kVMargin = 8.0f;
  31. const static CGFloat kFloatingLabelFontSize = 11.0f;
  32. @interface FloatLabeledTextFieldCell () <UITextFieldDelegate>
  33. @property (nonatomic) JVFloatLabeledTextField * floatLabeledTextField;
  34. @end
  35. @implementation FloatLabeledTextFieldCell
  36. @synthesize floatLabeledTextField =_floatLabeledTextField;
  37. +(void)load
  38. {
  39. [XLFormViewController.cellClassesForRowDescriptorTypes setObject:[FloatLabeledTextFieldCell class] forKey:XLFormRowDescriptorTypeFloatLabeledTextField];
  40. }
  41. -(JVFloatLabeledTextField *)floatLabeledTextField
  42. {
  43. if (_floatLabeledTextField) return _floatLabeledTextField;
  44. _floatLabeledTextField = [JVFloatLabeledTextField autolayoutView];
  45. _floatLabeledTextField.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
  46. _floatLabeledTextField.floatingLabel.font = [UIFont boldSystemFontOfSize:kFloatingLabelFontSize];
  47. _floatLabeledTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
  48. return _floatLabeledTextField;
  49. }
  50. #pragma mark - XLFormDescriptorCell
  51. -(void)configure
  52. {
  53. [super configure];
  54. [self setSelectionStyle:UITableViewCellSelectionStyleNone];
  55. [self.contentView addSubview:self.floatLabeledTextField];
  56. [self.floatLabeledTextField setDelegate:self];
  57. [self.contentView addConstraints:[self layoutConstraints]];
  58. }
  59. -(void)update
  60. {
  61. [super update];
  62. self.floatLabeledTextField.attributedPlaceholder =
  63. [[NSAttributedString alloc] initWithString:self.rowDescriptor.title
  64. attributes:@{NSForegroundColorAttributeName: [UIColor lightGrayColor]}];
  65. self.floatLabeledTextField.text = self.rowDescriptor.value ? [self.rowDescriptor.value displayText] : self.rowDescriptor.noValueDisplayText;
  66. [self.floatLabeledTextField setEnabled:!self.rowDescriptor.isDisabled];
  67. self.floatLabeledTextField.floatingLabelTextColor = [UIColor lightGrayColor];
  68. [self.floatLabeledTextField setAlpha:((self.rowDescriptor.isDisabled) ? .6 : 1)];
  69. }
  70. -(BOOL)formDescriptorCellCanBecomeFirstResponder
  71. {
  72. return !self.rowDescriptor.isDisabled;
  73. }
  74. -(BOOL)formDescriptorCellBecomeFirstResponder
  75. {
  76. return [self.floatLabeledTextField becomeFirstResponder];
  77. }
  78. #pragma mark - UITextFieldDelegate
  79. - (BOOL)textFieldShouldClear:(UITextField *)textField
  80. {
  81. return [self.formViewController textFieldShouldClear:textField];
  82. }
  83. - (BOOL)textFieldShouldReturn:(UITextField *)textField
  84. {
  85. return [self.formViewController textFieldShouldReturn:textField];
  86. }
  87. - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
  88. {
  89. return [self.formViewController textFieldShouldBeginEditing:textField];
  90. }
  91. - (BOOL)textFieldShouldEndEditing:(UITextField *)textField
  92. {
  93. return [self.formViewController textFieldShouldEndEditing:textField];
  94. }
  95. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  96. return [self.formViewController textField:textField shouldChangeCharactersInRange:range replacementString:string];
  97. }
  98. - (void)textFieldDidBeginEditing:(UITextField *)textField
  99. {
  100. [self.formViewController textFieldDidBeginEditing:textField];
  101. }
  102. - (void)textFieldDidEndEditing:(UITextField *)textField
  103. {
  104. [self textFieldDidChange:textField];
  105. [self.formViewController textFieldDidEndEditing:textField];
  106. }
  107. -(void)setReturnKeyType:(UIReturnKeyType)returnKeyType
  108. {
  109. self.floatLabeledTextField.returnKeyType = returnKeyType;
  110. }
  111. -(UIReturnKeyType)returnKeyType
  112. {
  113. return self.floatLabeledTextField.returnKeyType;
  114. }
  115. +(CGFloat)formDescriptorCellHeightForRowDescriptor:(XLFormRowDescriptor *)rowDescriptor {
  116. return 55;
  117. }
  118. -(NSArray *)layoutConstraints
  119. {
  120. NSMutableArray * result = [[NSMutableArray alloc] init];
  121. NSDictionary * views = @{@"floatLabeledTextField": self.floatLabeledTextField};
  122. NSDictionary *metrics = @{@"vMargin":@(kVMargin)};
  123. [result addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[floatLabeledTextField]-|"
  124. options:0
  125. metrics:metrics
  126. views:views]];
  127. [result addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(vMargin)-[floatLabeledTextField]-(vMargin)-|"
  128. options:0
  129. metrics:metrics
  130. views:views]];
  131. return result;
  132. }
  133. #pragma mark - Helpers
  134. - (void)textFieldDidChange:(UITextField *)textField
  135. {
  136. if (self.floatLabeledTextField == textField) {
  137. if ([self.floatLabeledTextField.text length] > 0) {
  138. self.rowDescriptor.value = self.floatLabeledTextField.text;
  139. } else {
  140. self.rowDescriptor.value = nil;
  141. }
  142. }
  143. }
  144. @end