FLEXFieldEditorView.m 5.8 KB

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