FLEXArgumentInputColorView.m 11 KB

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