XLFormStepCounterCell.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // XLFormStepCounterCell.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 "XLFormStepCounterCell.h"
  26. #import "XLFormRowDescriptor.h"
  27. #import "UIView+XLFormAdditions.h"
  28. @interface XLFormStepCounterCell ()
  29. @property (nonatomic) UIStepper *stepControl;
  30. @property (nonatomic) UILabel *currentStepValue;
  31. @end
  32. @implementation XLFormStepCounterCell
  33. #pragma mark - XLFormStepCounterCell
  34. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  35. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  36. if (self) {
  37. // Initialization code
  38. }
  39. return self;
  40. }
  41. - (void)configure
  42. {
  43. [super configure];
  44. self.selectionStyle = UITableViewCellSelectionStyleNone;
  45. // Add subviews
  46. [self.contentView addSubview:self.stepControl];
  47. [self.contentView addSubview:self.currentStepValue];
  48. // Add constraints
  49. [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.stepControl attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
  50. [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.currentStepValue attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
  51. [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.currentStepValue attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.stepControl attribute:NSLayoutAttributeHeight multiplier:1 constant:0]];
  52. [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[value]-5-[control]-|" options:0 metrics:0 views:@{@"value": self.currentStepValue, @"control":self.stepControl}]];
  53. }
  54. - (void)update
  55. {
  56. [super update];
  57. self.textLabel.text = self.rowDescriptor.title;
  58. self.stepControl.value = [self.rowDescriptor.value doubleValue];
  59. self.currentStepValue.text = self.rowDescriptor.value ? [NSString stringWithFormat:@"%@", self.rowDescriptor.value] : nil;
  60. [self stepControl].enabled = !self.rowDescriptor.isDisabled;
  61. [self currentStepValue].font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
  62. CGFloat red, green, blue, alpha;
  63. [self.tintColor getRed:&red green:&green blue:&blue alpha:&alpha];
  64. if (self.rowDescriptor.isDisabled)
  65. {
  66. [self setTintColor:[UIColor colorWithRed:red green:green blue:blue alpha:0.3]];
  67. [self currentStepValue].textColor = [UIColor colorWithRed:red green:green blue:blue alpha:0.3];
  68. }
  69. else{
  70. [self setTintColor:[UIColor colorWithRed:red green:green blue:blue alpha:1]];
  71. [self currentStepValue].textColor = [UIColor colorWithRed:red green:green blue:blue alpha:1];
  72. }
  73. }
  74. #pragma mark - Events
  75. - (void)valueChanged:(id)sender
  76. {
  77. UIStepper *stepper = self.stepControl;
  78. self.rowDescriptor.value = @(stepper.value);
  79. self.currentStepValue.text = [NSString stringWithFormat:@"%.f", stepper.value];
  80. }
  81. #pragma mark - Properties
  82. - (UIStepper *)stepControl
  83. {
  84. if (!_stepControl) {
  85. _stepControl = [UIStepper autolayoutView];
  86. [_stepControl addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
  87. }
  88. return _stepControl;
  89. }
  90. -(UILabel *)currentStepValue
  91. {
  92. if (!_currentStepValue) {
  93. _currentStepValue = [UILabel autolayoutView];
  94. }
  95. return _currentStepValue;
  96. }
  97. @end