You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.1 KiB

  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. self.inputSwitch.frame = CGRectMake(0, self.topInputFieldVerticalLayoutGuide, self.inputSwitch.frame.size.width, self.inputSwitch.frame.size.height);
  54. }
  55. - (CGSize)sizeThatFits:(CGSize)size
  56. {
  57. CGSize fitSize = [super sizeThatFits:size];
  58. fitSize.height += self.inputSwitch.frame.size.height;
  59. return fitSize;
  60. }
  61. #pragma mark - Class Helpers
  62. + (BOOL)supportsObjCType:(const char *)type withCurrentValue:(id)value
  63. {
  64. // Only BOOLs. Current value is irrelevant.
  65. return type && strcmp(type, @encode(BOOL)) == 0;
  66. }
  67. @end