FLEXFieldEditorView.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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)setTargetDescription:(NSString *)targetDescription
  64. {
  65. if (![_targetDescription isEqual:targetDescription]) {
  66. _targetDescription = targetDescription;
  67. self.targetDescriptionLabel.text = targetDescription;
  68. [self setNeedsLayout];
  69. }
  70. }
  71. - (void)setFieldDescription:(NSString *)fieldDescription
  72. {
  73. if (![_fieldDescription isEqual:fieldDescription]) {
  74. _fieldDescription = fieldDescription;
  75. self.fieldDescriptionLabel.text = fieldDescription;
  76. [self setNeedsLayout];
  77. }
  78. }
  79. - (void)setArgumentInputViews:(NSArray *)argumentInputViews
  80. {
  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. {
  94. UIView *dividerView = [[UIView alloc] init];
  95. dividerView.backgroundColor = [self dividerColor];
  96. return dividerView;
  97. }
  98. + (UIColor *)dividerColor
  99. {
  100. return [UIColor lightGrayColor];
  101. }
  102. + (CGFloat)horizontalPadding
  103. {
  104. return 10.0;
  105. }
  106. + (CGFloat)verticalPadding
  107. {
  108. return 20.0;
  109. }
  110. + (UIFont *)labelFont
  111. {
  112. return [FLEXUtility defaultFontOfSize:14.0];
  113. }
  114. + (CGFloat)dividerLineHeight
  115. {
  116. return 1.0;
  117. }
  118. - (CGSize)sizeThatFits:(CGSize)size
  119. {
  120. CGFloat horizontalPadding = [[self class] horizontalPadding];
  121. CGFloat verticalPadding = [[self class] verticalPadding];
  122. UIFont *font = [[self class] labelFont];
  123. CGFloat dividerLineHeight = [[self class] dividerLineHeight];
  124. CGFloat height = 0;
  125. CGFloat availableWidth = size.width - 2.0 * horizontalPadding;
  126. CGSize constrainSize = CGSizeMake(availableWidth, CGFLOAT_MAX);
  127. height += verticalPadding;
  128. height += ceil([self.targetDescription sizeWithFont:font constrainedToSize:constrainSize].height);
  129. height += verticalPadding;
  130. height += dividerLineHeight;
  131. height += verticalPadding;
  132. height += ceil([self.fieldDescription sizeWithFont:font constrainedToSize:constrainSize].height);
  133. height += verticalPadding;
  134. height += dividerLineHeight;
  135. height += verticalPadding;
  136. for (FLEXArgumentInputView *inputView in self.argumentInputViews) {
  137. height += [inputView sizeThatFits:constrainSize].height;
  138. height += verticalPadding;
  139. }
  140. return CGSizeMake(size.width, height);
  141. }
  142. @end