FLEXArgumentInputSwitchView.m 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // FLEXArgumentInputSwitchView.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 6/16/14.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXArgumentInputSwitchView.h"
  9. @interface FLEXArgumentInputSwitchView ()
  10. @property (nonatomic, strong) UISwitch *inputSwitch;
  11. @end
  12. @implementation FLEXArgumentInputSwitchView
  13. - (instancetype)initWithArgumentTypeEncoding:(const char *)typeEncoding
  14. {
  15. self = [super initWithArgumentTypeEncoding:typeEncoding];
  16. if (self) {
  17. self.inputSwitch = [[UISwitch alloc] init];
  18. [self.inputSwitch addTarget:self action:@selector(switchValueDidChange:) forControlEvents:UIControlEventValueChanged];
  19. [self.inputSwitch sizeToFit];
  20. [self addSubview:self.inputSwitch];
  21. }
  22. return self;
  23. }
  24. #pragma mark Input/Output
  25. - (void)setInputValue:(id)inputValue
  26. {
  27. BOOL on = NO;
  28. if ([inputValue isKindOfClass:[NSNumber class]]) {
  29. NSNumber *number = (NSNumber *)inputValue;
  30. on = [number boolValue];
  31. } else if ([inputValue isKindOfClass:[NSValue class]]) {
  32. NSValue *value = (NSValue *)inputValue;
  33. if (strcmp([value objCType], @encode(BOOL)) == 0) {
  34. [value getValue:&on];
  35. }
  36. }
  37. self.inputSwitch.on = on;
  38. }
  39. - (id)inputValue
  40. {
  41. BOOL isOn = [self.inputSwitch isOn];
  42. NSValue *boxedBool = [NSValue value:&isOn withObjCType:@encode(BOOL)];
  43. return boxedBool;
  44. }
  45. - (void)switchValueDidChange:(id)sender
  46. {
  47. [self.delegate argumentInputViewValueDidChange:self];
  48. }
  49. #pragma mark - Layout and Sizing
  50. - (void)layoutSubviews
  51. {
  52. [super layoutSubviews];
  53. CGFloat originY = 0;
  54. if (self.showsTitle) {
  55. originY = CGRectGetMaxY(self.titleLabel.frame) + [[self class] titleBottomPadding];
  56. }
  57. self.inputSwitch.frame = CGRectMake(0, originY, self.inputSwitch.frame.size.width, self.inputSwitch.frame.size.height);
  58. }
  59. - (CGSize)sizeThatFits:(CGSize)size
  60. {
  61. CGSize fitSize = [super sizeThatFits:size];
  62. fitSize.height += self.inputSwitch.frame.size.height;
  63. return fitSize;
  64. }
  65. #pragma mark - Class Helpers
  66. + (BOOL)supportsObjCType:(const char *)type withCurrentValue:(id)value
  67. {
  68. // Only BOOLs. Current value is irrelevant.
  69. return strcmp(type, @encode(BOOL)) == 0;
  70. }
  71. @end