XLFormTextViewCell.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //
  2. // XLFormTextViewCell.m
  3. // XLForm ( https://github.com/xmartlabs/XLForm )
  4. //
  5. // Copyright (c) 2015 Xmartlabs ( http://xmartlabs.com )
  6. //
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in
  16. // all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. // THE SOFTWARE.
  25. #import "XLFormRowDescriptor.h"
  26. #import "UIView+XLFormAdditions.h"
  27. #import "XLFormViewController.h"
  28. #import "XLFormTextView.h"
  29. #import "XLFormTextViewCell.h"
  30. NSString *const XLFormTextViewLengthPercentage = @"textViewLengthPercentage";
  31. NSString *const XLFormTextViewMaxNumberOfCharacters = @"textViewMaxNumberOfCharacters";
  32. @interface XLFormTextViewCell() <UITextViewDelegate>
  33. @end
  34. @implementation XLFormTextViewCell
  35. {
  36. NSMutableArray * _dynamicCustomConstraints;
  37. }
  38. @synthesize textLabel = _textLabel;
  39. @synthesize textView = _textView;
  40. -(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  41. {
  42. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  43. if (self) {
  44. _dynamicCustomConstraints = [NSMutableArray array];
  45. }
  46. return self;
  47. }
  48. -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  49. {
  50. if (object == self.textLabel && [keyPath isEqualToString:@"text"]){
  51. if ([[change objectForKey:NSKeyValueChangeKindKey] isEqualToNumber:@(NSKeyValueChangeSetting)]){
  52. [self needsUpdateConstraints];
  53. }
  54. }
  55. }
  56. -(void)dealloc
  57. {
  58. [self.textLabel removeObserver:self forKeyPath:@"text"];
  59. }
  60. #pragma mark - Properties
  61. -(UILabel *)textLabel
  62. {
  63. if (_textLabel) return _textLabel;
  64. _textLabel = [UILabel autolayoutView];
  65. [_textLabel setContentHuggingPriority:500 forAxis:UILayoutConstraintAxisHorizontal];
  66. return _textLabel;
  67. }
  68. -(UILabel *)label
  69. {
  70. return self.textLabel;
  71. }
  72. -(XLFormTextView *)textView
  73. {
  74. if (_textView) return _textView;
  75. _textView = [XLFormTextView autolayoutView];
  76. return _textView;
  77. }
  78. #pragma mark - XLFormDescriptorCell
  79. -(void)configure
  80. {
  81. [super configure];
  82. [self setSelectionStyle:UITableViewCellSelectionStyleNone];
  83. [self.contentView addSubview:self.textLabel];
  84. [self.contentView addSubview:self.textView];
  85. [self.textLabel addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:0];
  86. NSDictionary * views = @{@"label": self.textLabel, @"textView": self.textView};
  87. [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-8-[label]" options:0 metrics:0 views:views]];
  88. [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.textView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1 constant:0]];
  89. [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.textView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];
  90. [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[textView]-0-|" options:0 metrics:0 views:views]];
  91. }
  92. -(void)update
  93. {
  94. [super update];
  95. self.textView.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
  96. self.textView.placeHolderLabel.font = self.textView.font;
  97. self.textView.delegate = self;
  98. self.textView.keyboardType = UIKeyboardTypeDefault;
  99. self.textView.text = self.rowDescriptor.value;
  100. [self.textView setEditable:!self.rowDescriptor.isDisabled];
  101. self.textView.textColor = self.rowDescriptor.isDisabled ? [UIColor grayColor] : [UIColor blackColor];
  102. self.textLabel.text = ((self.rowDescriptor.required && self.rowDescriptor.title && self.rowDescriptor.sectionDescriptor.formDescriptor.addAsteriskToRequiredRowsTitle) ? [NSString stringWithFormat:@"%@*", self.rowDescriptor.title]: self.rowDescriptor.title);
  103. }
  104. +(CGFloat)formDescriptorCellHeightForRowDescriptor:(XLFormRowDescriptor *)rowDescriptor
  105. {
  106. return 110.f;
  107. }
  108. -(BOOL)formDescriptorCellCanBecomeFirstResponder
  109. {
  110. return (!self.rowDescriptor.isDisabled);
  111. }
  112. -(BOOL)formDescriptorCellBecomeFirstResponder
  113. {
  114. return [self.textView becomeFirstResponder];
  115. }
  116. -(void)highlight
  117. {
  118. [super highlight];
  119. self.textLabel.textColor = self.tintColor;
  120. }
  121. -(void)unhighlight
  122. {
  123. [super unhighlight];
  124. [self.formViewController updateFormRow:self.rowDescriptor];
  125. }
  126. #pragma mark - Constraints
  127. -(void)updateConstraints
  128. {
  129. if (_dynamicCustomConstraints){
  130. [self.contentView removeConstraints:_dynamicCustomConstraints];
  131. [_dynamicCustomConstraints removeAllObjects];
  132. }
  133. NSDictionary * views = @{@"label": self.textLabel, @"textView": self.textView};
  134. if (!self.textLabel.text || [self.textLabel.text isEqualToString:@""]){
  135. [_dynamicCustomConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[textView]-|" options:0 metrics:0 views:views]];
  136. }
  137. else{
  138. [_dynamicCustomConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[label]-[textView]-|" options:0 metrics:0 views:views]];
  139. if (self.textViewLengthPercentage) {
  140. [_dynamicCustomConstraints addObject:[NSLayoutConstraint constraintWithItem:_textView
  141. attribute:NSLayoutAttributeWidth
  142. relatedBy:NSLayoutRelationEqual
  143. toItem:self.contentView
  144. attribute:NSLayoutAttributeWidth
  145. multiplier:[self.textViewLengthPercentage floatValue]
  146. constant:0.0]];
  147. }
  148. }
  149. [self.contentView addConstraints:_dynamicCustomConstraints];
  150. [super updateConstraints];
  151. }
  152. #pragma mark - UITextViewDelegate
  153. -(void)textViewDidBeginEditing:(UITextView *)textView
  154. {
  155. [self.formViewController beginEditing:self.rowDescriptor];
  156. return [self.formViewController textViewDidBeginEditing:textView];
  157. }
  158. -(void)textViewDidEndEditing:(UITextView *)textView
  159. {
  160. if ([self.textView.text length] > 0) {
  161. self.rowDescriptor.value = self.textView.text;
  162. } else {
  163. self.rowDescriptor.value = nil;
  164. }
  165. [self.formViewController endEditing:self.rowDescriptor];
  166. [self.formViewController textViewDidEndEditing:textView];
  167. }
  168. - (BOOL)textViewShouldBeginEditing:(UITextView *)textView
  169. {
  170. return [self.formViewController textViewShouldBeginEditing:textView];
  171. }
  172. -(void)textViewDidChange:(UITextView *)textView{
  173. if ([self.textView.text length] > 0) {
  174. self.rowDescriptor.value = self.textView.text;
  175. } else {
  176. self.rowDescriptor.value = nil;
  177. }
  178. }
  179. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
  180. if (self.textViewMaxNumberOfCharacters) {
  181. // Check maximum length requirement
  182. NSString *newText = [textView.text stringByReplacingCharactersInRange:range withString:text];
  183. if (newText.length > self.textViewMaxNumberOfCharacters.integerValue) {
  184. return NO;
  185. }
  186. }
  187. // Otherwise, leave response to view controller
  188. return [self.formViewController textView:textView shouldChangeTextInRange:range replacementText:text];
  189. }
  190. @end