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