XLFormWeekDaysCell.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // XLFormWeekDaysCell.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 "XLFormWeekDaysCell.h"
  25. NSString * const XLFormRowDescriptorTypeWeekDays = @"XLFormRowDescriptorTypeWeekDays";
  26. NSString *const kSunday= @"sunday";
  27. NSString *const kMonday = @"monday";
  28. NSString *const kTuesday = @"tuesday";
  29. NSString *const kWednesday = @"wednesday";
  30. NSString *const kThursday = @"thursday";
  31. NSString *const kFriday = @"friday";
  32. NSString *const kSaturday = @"saturday";
  33. @interface XLFormWeekDaysCell()
  34. @property (weak, nonatomic) IBOutlet UIButton *sundayButton;
  35. @property (weak, nonatomic) IBOutlet UIButton *mondayButton;
  36. @property (weak, nonatomic) IBOutlet UIButton *tuesdayButton;
  37. @property (weak, nonatomic) IBOutlet UIButton *wednesdayButton;
  38. @property (weak, nonatomic) IBOutlet UIButton *thursdayButton;
  39. @property (weak, nonatomic) IBOutlet UIButton *fridayButton;
  40. @property (weak, nonatomic) IBOutlet UIButton *saturdayButton;
  41. @end
  42. @implementation XLFormWeekDaysCell
  43. +(void)load
  44. {
  45. [XLFormViewController.cellClassesForRowDescriptorTypes setObject:NSStringFromClass([XLFormWeekDaysCell class]) forKey:XLFormRowDescriptorTypeWeekDays];
  46. }
  47. #pragma mark - XLFormDescriptorCell
  48. - (void)configure
  49. {
  50. [super configure];
  51. self.selectionStyle = UITableViewCellSelectionStyleNone;
  52. [self configureButtons];
  53. }
  54. -(void)update
  55. {
  56. [super update];
  57. [self updateButtons];
  58. }
  59. #pragma mark - Action
  60. - (IBAction)dayTapped:(id)sender {
  61. [self dayTapped:sender day:[self getDayFormButton:sender]];
  62. }
  63. #pragma mark - Helpers
  64. -(void)configureButtons
  65. {
  66. for (UIView *subview in self.contentView.subviews)
  67. {
  68. if ([subview isKindOfClass:[UIButton class]])
  69. {
  70. UIButton * button = (UIButton *)subview;
  71. [button setImage:[UIImage imageNamed:@"uncheckedDay"] forState:UIControlStateNormal];
  72. [button setImage:[UIImage imageNamed:@"checkedDay"] forState:UIControlStateSelected];
  73. button.adjustsImageWhenHighlighted = NO;
  74. [self imageTopTitleBottom:button];
  75. }
  76. }
  77. }
  78. -(void)updateButtons
  79. {
  80. NSDictionary * value = self.rowDescriptor.value;
  81. self.sundayButton.selected = [[value objectForKey:kSunday] boolValue];
  82. self.mondayButton.selected = [[value objectForKey:kMonday] boolValue];
  83. self.tuesdayButton.selected = [[value objectForKey:kTuesday] boolValue];
  84. self.wednesdayButton.selected = [[value objectForKey:kWednesday] boolValue];
  85. self.thursdayButton.selected = [[value objectForKey:kThursday] boolValue];
  86. self.fridayButton.selected = [[value objectForKey:kFriday] boolValue];
  87. self.saturdayButton.selected = [[value objectForKey:kSaturday] boolValue];
  88. [self.sundayButton setAlpha:((self.rowDescriptor.isDisabled) ? .6 : 1)];
  89. [self.mondayButton setAlpha:self.sundayButton.alpha];
  90. [self.tuesdayButton setAlpha:self.sundayButton.alpha];
  91. [self.wednesdayButton setAlpha:self.sundayButton.alpha];
  92. [self.thursdayButton setAlpha:self.sundayButton.alpha];
  93. [self.fridayButton setAlpha:self.sundayButton.alpha];
  94. [self.saturdayButton setAlpha:self.sundayButton.alpha];
  95. }
  96. -(NSString *)getDayFormButton:(id)sender
  97. {
  98. if (sender == self.sundayButton) return kSunday;
  99. if (sender == self.mondayButton) return kMonday;
  100. if (sender == self.tuesdayButton) return kTuesday;
  101. if (sender == self.wednesdayButton) return kWednesday;
  102. if (sender == self.thursdayButton) return kThursday;
  103. if (sender == self.fridayButton) return kFriday;
  104. return kSaturday;
  105. }
  106. -(void)dayTapped:(UIButton *)button day:(NSString *)day
  107. {
  108. button.selected = !button.selected;
  109. NSMutableDictionary * value = [self.rowDescriptor.value mutableCopy];
  110. [value setObject:@(button.selected) forKey:day];
  111. self.rowDescriptor.value = value;
  112. }
  113. -(void)imageTopTitleBottom:(UIButton *)button
  114. {
  115. // the space between the image and text
  116. CGFloat spacing = 3.0;
  117. // lower the text and push it left so it appears centered
  118. // below the image
  119. CGSize imageSize = button.imageView.image.size;
  120. button.titleEdgeInsets = UIEdgeInsetsMake(0.0, - imageSize.width, - (imageSize.height + spacing), 0.0);
  121. // raise the image and push it right so it appears centered
  122. // above the text
  123. CGSize titleSize = [button.titleLabel.text sizeWithAttributes:@{NSFontAttributeName: button.titleLabel.font}];
  124. button.imageEdgeInsets = UIEdgeInsetsMake(- (titleSize.height + spacing), 0.0, 0.0, - titleSize.width);
  125. }
  126. +(CGFloat)formDescriptorCellHeightForRowDescriptor:(XLFormRowDescriptor *)rowDescriptor
  127. {
  128. return 60;
  129. }
  130. @end