FLEXArgumentInputColorView.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. //
  2. // FLEXArgumentInputColorView.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 6/30/14.
  6. // Copyright (c) 2020 FLEX Team. All rights reserved.
  7. //
  8. #import "FLEXArgumentInputColorView.h"
  9. #import "FLEXUtility.h"
  10. #import "FLEXRuntimeUtility.h"
  11. #import "fakes.h"
  12. @protocol FLEXColorComponentInputViewDelegate;
  13. @interface FLEXColorComponentInputView : UIView
  14. #if !TARGET_OS_TV
  15. @property (nonatomic) UISlider *slider;
  16. #else
  17. @property (nonatomic) KBSlider *slider;
  18. #endif
  19. @property (nonatomic) UILabel *valueLabel;
  20. @property (nonatomic, weak) id <FLEXColorComponentInputViewDelegate> delegate;
  21. @end
  22. @protocol FLEXColorComponentInputViewDelegate <NSObject>
  23. - (void)colorComponentInputViewValueDidChange:(FLEXColorComponentInputView *)colorComponentInputView;
  24. @end
  25. @implementation FLEXColorComponentInputView
  26. - (id)initWithFrame:(CGRect)frame {
  27. self = [super initWithFrame:frame];
  28. if (self) {
  29. #if !TARGET_OS_TV
  30. self.slider = [UISlider new];
  31. #else
  32. self.slider = [[KBSlider alloc] initWithFrame:CGRectMake(0, 0, 1000, 53)];
  33. #endif
  34. [self.slider addTarget:self action:@selector(sliderChanged:) forControlEvents:UIControlEventValueChanged];
  35. [self addSubview:self.slider];
  36. self.valueLabel = [UILabel new];
  37. self.valueLabel.backgroundColor = self.backgroundColor;
  38. self.valueLabel.font = [UIFont systemFontOfSize:14.0];
  39. self.valueLabel.textAlignment = NSTextAlignmentRight;
  40. [self addSubview:self.valueLabel];
  41. [self updateValueLabel];
  42. }
  43. return self;
  44. }
  45. - (void)setBackgroundColor:(UIColor *)backgroundColor {
  46. [super setBackgroundColor:backgroundColor];
  47. self.slider.backgroundColor = backgroundColor;
  48. self.valueLabel.backgroundColor = backgroundColor;
  49. }
  50. - (void)layoutSubviews {
  51. [super layoutSubviews];
  52. const CGFloat kValueLabelWidth = 50.0;
  53. [self.slider sizeToFit];
  54. CGFloat sliderWidth = self.bounds.size.width - kValueLabelWidth;
  55. self.slider.frame = CGRectMake(0, 0, sliderWidth, self.slider.frame.size.height);
  56. [self.valueLabel sizeToFit];
  57. CGFloat valueLabelOriginX = CGRectGetMaxX(self.slider.frame);
  58. CGFloat valueLabelOriginY = FLEXFloor((self.slider.frame.size.height - self.valueLabel.frame.size.height) / 2.0);
  59. self.valueLabel.frame = CGRectMake(valueLabelOriginX, valueLabelOriginY, kValueLabelWidth, self.valueLabel.frame.size.height);
  60. }
  61. - (void)sliderChanged:(id)sender {
  62. [self.delegate colorComponentInputViewValueDidChange:self];
  63. [self updateValueLabel];
  64. }
  65. - (void)updateValueLabel {
  66. self.valueLabel.text = [NSString stringWithFormat:@"%.3f", self.slider.value];
  67. }
  68. - (CGSize)sizeThatFits:(CGSize)size {
  69. CGFloat height = [self.slider sizeThatFits:size].height;
  70. return CGSizeMake(size.width, height);
  71. }
  72. @end
  73. @interface FLEXColorPreviewBox : UIView
  74. @property (nonatomic) UIColor *color;
  75. @property (nonatomic) UIView *colorOverlayView;
  76. @end
  77. @implementation FLEXColorPreviewBox
  78. - (id)initWithFrame:(CGRect)frame {
  79. self = [super initWithFrame:frame];
  80. if (self) {
  81. self.layer.borderWidth = 1.0;
  82. self.layer.borderColor = UIColor.blackColor.CGColor;
  83. self.backgroundColor = [UIColor colorWithPatternImage:[[self class] backgroundPatternImage]];
  84. self.colorOverlayView = [[UIView alloc] initWithFrame:self.bounds];
  85. self.colorOverlayView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  86. self.colorOverlayView.backgroundColor = UIColor.clearColor;
  87. [self addSubview:self.colorOverlayView];
  88. }
  89. return self;
  90. }
  91. - (void)setColor:(UIColor *)color {
  92. self.colorOverlayView.backgroundColor = color;
  93. }
  94. - (UIColor *)color {
  95. return self.colorOverlayView.backgroundColor;
  96. }
  97. + (UIImage *)backgroundPatternImage {
  98. const CGFloat kSquareDimension = 5.0;
  99. CGSize squareSize = CGSizeMake(kSquareDimension, kSquareDimension);
  100. CGSize imageSize = CGSizeMake(2.0 * kSquareDimension, 2.0 * kSquareDimension);
  101. UIGraphicsBeginImageContextWithOptions(imageSize, YES, UIScreen.mainScreen.scale);
  102. [UIColor.whiteColor setFill];
  103. UIRectFill(CGRectMake(0, 0, imageSize.width, imageSize.height));
  104. [UIColor.grayColor setFill];
  105. UIRectFill(CGRectMake(squareSize.width, 0, squareSize.width, squareSize.height));
  106. UIRectFill(CGRectMake(0, squareSize.height, squareSize.width, squareSize.height));
  107. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  108. UIGraphicsEndImageContext();
  109. return image;
  110. }
  111. @end
  112. @interface FLEXArgumentInputColorView () <FLEXColorComponentInputViewDelegate>
  113. @property (nonatomic) FLEXColorPreviewBox *colorPreviewBox;
  114. @property (nonatomic) UILabel *hexLabel;
  115. @property (nonatomic) FLEXColorComponentInputView *alphaInput;
  116. @property (nonatomic) FLEXColorComponentInputView *redInput;
  117. @property (nonatomic) FLEXColorComponentInputView *greenInput;
  118. @property (nonatomic) FLEXColorComponentInputView *blueInput;
  119. @end
  120. @implementation FLEXArgumentInputColorView
  121. - (instancetype)initWithArgumentTypeEncoding:(const char *)typeEncoding {
  122. self = [super initWithArgumentTypeEncoding:typeEncoding];
  123. if (self) {
  124. self.colorPreviewBox = [FLEXColorPreviewBox new];
  125. [self addSubview:self.colorPreviewBox];
  126. self.hexLabel = [UILabel new];
  127. self.hexLabel.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.9];
  128. self.hexLabel.textAlignment = NSTextAlignmentCenter;
  129. self.hexLabel.font = [UIFont systemFontOfSize:12.0];
  130. [self addSubview:self.hexLabel];
  131. self.alphaInput = [FLEXColorComponentInputView new];
  132. self.alphaInput.slider.minimumTrackTintColor = UIColor.blackColor;
  133. self.alphaInput.delegate = self;
  134. [self addSubview:self.alphaInput];
  135. self.redInput = [FLEXColorComponentInputView new];
  136. self.redInput.slider.minimumTrackTintColor = UIColor.redColor;
  137. self.redInput.delegate = self;
  138. [self addSubview:self.redInput];
  139. self.greenInput = [FLEXColorComponentInputView new];
  140. self.greenInput.slider.minimumTrackTintColor = UIColor.greenColor;
  141. self.greenInput.delegate = self;
  142. [self addSubview:self.greenInput];
  143. self.blueInput = [FLEXColorComponentInputView new];
  144. self.blueInput.slider.minimumTrackTintColor = UIColor.blueColor;
  145. self.blueInput.delegate = self;
  146. [self addSubview:self.blueInput];
  147. }
  148. return self;
  149. }
  150. - (void)setBackgroundColor:(UIColor *)backgroundColor {
  151. [super setBackgroundColor:backgroundColor];
  152. self.alphaInput.backgroundColor = backgroundColor;
  153. self.redInput.backgroundColor = backgroundColor;
  154. self.greenInput.backgroundColor = backgroundColor;
  155. self.blueInput.backgroundColor = backgroundColor;
  156. }
  157. - (void)layoutSubviews {
  158. [super layoutSubviews];
  159. CGFloat runningOriginY = 0;
  160. CGSize constrainSize = CGSizeMake(self.bounds.size.width, CGFLOAT_MAX);
  161. self.colorPreviewBox.frame = CGRectMake(0, runningOriginY, self.bounds.size.width, [[self class] colorPreviewBoxHeight]);
  162. runningOriginY = CGRectGetMaxY(self.colorPreviewBox.frame) + [[self class] inputViewVerticalPadding];
  163. [self.hexLabel sizeToFit];
  164. const CGFloat kLabelVerticalOutsetAmount = 0.0;
  165. const CGFloat kLabelHorizontalOutsetAmount = 2.0;
  166. UIEdgeInsets labelOutset = UIEdgeInsetsMake(-kLabelVerticalOutsetAmount, -kLabelHorizontalOutsetAmount, -kLabelVerticalOutsetAmount, -kLabelHorizontalOutsetAmount);
  167. self.hexLabel.frame = UIEdgeInsetsInsetRect(self.hexLabel.frame, labelOutset);
  168. CGFloat hexLabelOriginX = self.colorPreviewBox.layer.borderWidth;
  169. CGFloat hexLabelOriginY = CGRectGetMaxY(self.colorPreviewBox.frame) - self.colorPreviewBox.layer.borderWidth - self.hexLabel.frame.size.height;
  170. self.hexLabel.frame = CGRectMake(hexLabelOriginX, hexLabelOriginY, self.hexLabel.frame.size.width, self.hexLabel.frame.size.height);
  171. NSArray<FLEXColorComponentInputView *> *colorComponentInputViews = @[self.alphaInput, self.redInput, self.greenInput, self.blueInput];
  172. for (FLEXColorComponentInputView *inputView in colorComponentInputViews) {
  173. CGSize fitSize = [inputView sizeThatFits:constrainSize];
  174. inputView.frame = CGRectMake(0, runningOriginY, fitSize.width, fitSize.height);
  175. runningOriginY = CGRectGetMaxY(inputView.frame) + [[self class] inputViewVerticalPadding];
  176. }
  177. }
  178. - (void)setInputValue:(id)inputValue {
  179. if ([inputValue isKindOfClass:[UIColor class]]) {
  180. [self updateWithColor:inputValue];
  181. } else if ([inputValue isKindOfClass:[NSValue class]]) {
  182. const char *type = [inputValue objCType];
  183. if (strcmp(type, @encode(CGColorRef)) == 0) {
  184. CGColorRef colorRef;
  185. [inputValue getValue:&colorRef];
  186. UIColor *color = [[UIColor alloc] initWithCGColor:colorRef];
  187. [self updateWithColor:color];
  188. }
  189. } else {
  190. [self updateWithColor:UIColor.clearColor];
  191. }
  192. }
  193. - (id)inputValue {
  194. return [UIColor colorWithRed:self.redInput.slider.value green:self.greenInput.slider.value blue:self.blueInput.slider.value alpha:self.alphaInput.slider.value];
  195. }
  196. - (void)colorComponentInputViewValueDidChange:(FLEXColorComponentInputView *)colorComponentInputView {
  197. [self updateColorPreview];
  198. }
  199. - (void)updateWithColor:(UIColor *)color {
  200. CGFloat red, green, blue, white, alpha;
  201. if ([color getRed:&red green:&green blue:&blue alpha:&alpha]) {
  202. self.alphaInput.slider.value = alpha;
  203. [self.alphaInput updateValueLabel];
  204. self.redInput.slider.value = red;
  205. [self.redInput updateValueLabel];
  206. self.greenInput.slider.value = green;
  207. [self.greenInput updateValueLabel];
  208. self.blueInput.slider.value = blue;
  209. [self.blueInput updateValueLabel];
  210. } else if ([color getWhite:&white alpha:&alpha]) {
  211. self.alphaInput.slider.value = alpha;
  212. [self.alphaInput updateValueLabel];
  213. self.redInput.slider.value = white;
  214. [self.redInput updateValueLabel];
  215. self.greenInput.slider.value = white;
  216. [self.greenInput updateValueLabel];
  217. self.blueInput.slider.value = white;
  218. [self.blueInput updateValueLabel];
  219. }
  220. [self updateColorPreview];
  221. }
  222. - (void)updateColorPreview {
  223. self.colorPreviewBox.color = self.inputValue;
  224. unsigned char redByte = self.redInput.slider.value * 255;
  225. unsigned char greenByte = self.greenInput.slider.value * 255;
  226. unsigned char blueByte = self.blueInput.slider.value * 255;
  227. self.hexLabel.text = [NSString stringWithFormat:@"#%02X%02X%02X", redByte, greenByte, blueByte];
  228. [self setNeedsLayout];
  229. }
  230. - (CGSize)sizeThatFits:(CGSize)size {
  231. CGFloat height = 0;
  232. height += [[self class] colorPreviewBoxHeight];
  233. height += [[self class] inputViewVerticalPadding];
  234. height += [self.alphaInput sizeThatFits:size].height;
  235. height += [[self class] inputViewVerticalPadding];
  236. height += [self.redInput sizeThatFits:size].height;
  237. height += [[self class] inputViewVerticalPadding];
  238. height += [self.greenInput sizeThatFits:size].height;
  239. height += [[self class] inputViewVerticalPadding];
  240. height += [self.blueInput sizeThatFits:size].height;
  241. return CGSizeMake(size.width, height);
  242. }
  243. + (CGFloat)inputViewVerticalPadding {
  244. return 10.0;
  245. }
  246. + (CGFloat)colorPreviewBoxHeight {
  247. return 40.0;
  248. }
  249. + (BOOL)supportsObjCType:(const char *)type withCurrentValue:(id)value {
  250. NSParameterAssert(type);
  251. // We don't care if currentValue is a color or not; we will default to +clearColor
  252. return (strcmp(type, @encode(CGColorRef)) == 0) || (strcmp(type, FLEXEncodeClass(UIColor)) == 0);
  253. }
  254. @end