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.

184 lines
5.9 KiB

  1. //
  2. // FLEXFieldEditorView.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 5/16/14.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXFieldEditorView.h"
  9. #import "FLEXArgumentInputView.h"
  10. #import "FLEXUtility.h"
  11. @interface FLEXFieldEditorView ()
  12. @property (nonatomic, strong) UILabel *targetDescriptionLabel;
  13. @property (nonatomic, strong) UIView *targetDescriptionDivider;
  14. @property (nonatomic, strong) UILabel *fieldDescriptionLabel;
  15. @property (nonatomic, strong) UIView *fieldDescriptionDivider;
  16. @end
  17. @implementation FLEXFieldEditorView
  18. - (id)initWithFrame:(CGRect)frame
  19. {
  20. self = [super initWithFrame:frame];
  21. if (self) {
  22. self.targetDescriptionLabel = [[UILabel alloc] init];
  23. self.targetDescriptionLabel.numberOfLines = 0;
  24. self.targetDescriptionLabel.font = [[self class] labelFont];
  25. [self addSubview:self.targetDescriptionLabel];
  26. self.targetDescriptionDivider = [[self class] dividerView];
  27. [self addSubview:self.targetDescriptionDivider];
  28. self.fieldDescriptionLabel = [[UILabel alloc] init];
  29. self.fieldDescriptionLabel.numberOfLines = 0;
  30. self.fieldDescriptionLabel.font = [[self class] labelFont];
  31. [self addSubview:self.fieldDescriptionLabel];
  32. self.fieldDescriptionDivider = [[self class] dividerView];
  33. [self addSubview:self.fieldDescriptionDivider];
  34. }
  35. return self;
  36. }
  37. - (void)layoutSubviews
  38. {
  39. [super layoutSubviews];
  40. CGFloat horizontalPadding = [[self class] horizontalPadding];
  41. CGFloat verticalPadding = [[self class] verticalPadding];
  42. CGFloat dividerLineHeight = [[self class] dividerLineHeight];
  43. CGFloat originY = verticalPadding;
  44. CGFloat originX = horizontalPadding;
  45. CGFloat contentWidth = self.bounds.size.width - 2.0 * horizontalPadding;
  46. CGSize constrainSize = CGSizeMake(contentWidth, CGFLOAT_MAX);
  47. CGSize instanceDescriptionSize = [self.targetDescriptionLabel sizeThatFits:constrainSize];
  48. self.targetDescriptionLabel.frame = CGRectMake(originX, originY, instanceDescriptionSize.width, instanceDescriptionSize.height);
  49. originY = CGRectGetMaxY(self.targetDescriptionLabel.frame) + verticalPadding;
  50. self.targetDescriptionDivider.frame = CGRectMake(originX, originY, contentWidth, dividerLineHeight);
  51. originY = CGRectGetMaxY(self.targetDescriptionDivider.frame) + verticalPadding;
  52. CGSize fieldDescriptionSize = [self.fieldDescriptionLabel sizeThatFits:constrainSize];
  53. self.fieldDescriptionLabel.frame = CGRectMake(originX, originY, fieldDescriptionSize.width, fieldDescriptionSize.height);
  54. originY = CGRectGetMaxY(self.fieldDescriptionLabel.frame) + verticalPadding;
  55. self.fieldDescriptionDivider.frame = CGRectMake(originX, originY, contentWidth, dividerLineHeight);
  56. originY = CGRectGetMaxY(self.fieldDescriptionDivider.frame) + verticalPadding;
  57. for (UIView *argumentInputView in self.argumentInputViews) {
  58. CGSize inputViewSize = [argumentInputView sizeThatFits:constrainSize];
  59. argumentInputView.frame = CGRectMake(originX, originY, inputViewSize.width, inputViewSize.height);
  60. originY = CGRectGetMaxY(argumentInputView.frame) + verticalPadding;
  61. }
  62. }
  63. - (void)setBackgroundColor:(UIColor *)backgroundColor
  64. {
  65. [super setBackgroundColor:backgroundColor];
  66. self.targetDescriptionLabel.backgroundColor = backgroundColor;
  67. self.fieldDescriptionLabel.backgroundColor = backgroundColor;
  68. }
  69. - (void)setTargetDescription:(NSString *)targetDescription
  70. {
  71. if (![_targetDescription isEqual:targetDescription]) {
  72. _targetDescription = targetDescription;
  73. self.targetDescriptionLabel.text = targetDescription;
  74. [self setNeedsLayout];
  75. }
  76. }
  77. - (void)setFieldDescription:(NSString *)fieldDescription
  78. {
  79. if (![_fieldDescription isEqual:fieldDescription]) {
  80. _fieldDescription = fieldDescription;
  81. self.fieldDescriptionLabel.text = fieldDescription;
  82. [self setNeedsLayout];
  83. }
  84. }
  85. - (void)setArgumentInputViews:(NSArray<FLEXArgumentInputView *> *)argumentInputViews
  86. {
  87. if (![_argumentInputViews isEqual:argumentInputViews]) {
  88. for (FLEXArgumentInputView *inputView in _argumentInputViews) {
  89. [inputView removeFromSuperview];
  90. }
  91. _argumentInputViews = argumentInputViews;
  92. for (FLEXArgumentInputView *newInputView in argumentInputViews) {
  93. [self addSubview:newInputView];
  94. }
  95. [self setNeedsLayout];
  96. }
  97. }
  98. + (UIView *)dividerView
  99. {
  100. UIView *dividerView = [[UIView alloc] init];
  101. dividerView.backgroundColor = [self dividerColor];
  102. return dividerView;
  103. }
  104. + (UIColor *)dividerColor
  105. {
  106. return [UIColor lightGrayColor];
  107. }
  108. + (CGFloat)horizontalPadding
  109. {
  110. return 10.0;
  111. }
  112. + (CGFloat)verticalPadding
  113. {
  114. return 20.0;
  115. }
  116. + (UIFont *)labelFont
  117. {
  118. return [FLEXUtility defaultFontOfSize:14.0];
  119. }
  120. + (CGFloat)dividerLineHeight
  121. {
  122. return 1.0;
  123. }
  124. - (CGSize)sizeThatFits:(CGSize)size
  125. {
  126. CGFloat horizontalPadding = [[self class] horizontalPadding];
  127. CGFloat verticalPadding = [[self class] verticalPadding];
  128. CGFloat dividerLineHeight = [[self class] dividerLineHeight];
  129. CGFloat height = 0;
  130. CGFloat availableWidth = size.width - 2.0 * horizontalPadding;
  131. CGSize constrainSize = CGSizeMake(availableWidth, CGFLOAT_MAX);
  132. height += verticalPadding;
  133. height += ceil([self.targetDescriptionLabel sizeThatFits:constrainSize].height);
  134. height += verticalPadding;
  135. height += dividerLineHeight;
  136. height += verticalPadding;
  137. height += ceil([self.fieldDescriptionLabel sizeThatFits:constrainSize].height);
  138. height += verticalPadding;
  139. height += dividerLineHeight;
  140. height += verticalPadding;
  141. for (FLEXArgumentInputView *inputView in self.argumentInputViews) {
  142. height += [inputView sizeThatFits:constrainSize].height;
  143. height += verticalPadding;
  144. }
  145. return CGSizeMake(size.width, height);
  146. }
  147. @end