FLEXArgumentInputColorView.m 11 KB

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