-
Notifications
You must be signed in to change notification settings - Fork 34
/
FeedItemManualView.swift
executable file
·211 lines (168 loc) · 7.52 KB
/
FeedItemManualView.swift
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright 2016 LinkedIn Corp.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
import UIKit
/// A LinkedIn feed item that is implemented with manual layout code.
class FeedItemManualView: UIView, DataBinder {
let hMargin: CGFloat = 8
let actionLabel: UILabel = {
let l = UILabel()
return l
}()
let optionsLabel: UILabel = {
let l = UILabel()
l.text = "..."
l.sizeToFit()
return l
}()
let posterImageView: UIImageView = {
let i = UIImageView()
i.image = UIImage(named: "50x50.png")
i.backgroundColor = UIColor.orange
i.contentMode = .scaleToFill
i.sizeToFit()
return i
}()
let posterNameLabel: UILabel = {
let l = UILabel()
l.backgroundColor = UIColor.yellow
return l
}()
let posterHeadlineLabel: UILabel = {
let l = UILabel()
l.backgroundColor = UIColor.yellow
return l
}()
let posterTimeLabel: UILabel = {
let l = UILabel()
l.backgroundColor = UIColor.yellow
return l
}()
let posterCommentLabel: UILabel = UILabel()
let contentImageView: UIImageView = {
let i = UIImageView()
i.image = UIImage(named: "350x200.png")
i.contentMode = .scaleToFill
i.sizeToFit()
return i
}()
let contentTitleLabel: UILabel = UILabel()
let contentDomainLabel: UILabel = UILabel()
let likeLabel: UILabel = {
let l = UILabel()
l.backgroundColor = .green
l.text = "Like"
return l
}()
let commentLabel: UILabel = {
let l = UILabel()
l.text = "Comment"
l.backgroundColor = .green
l.textAlignment = .center
return l
}()
let shareLabel: UILabel = {
let l = UILabel()
l.text = "Share"
l.backgroundColor = .green
l.textAlignment = .right
return l
}()
let actorImageView: UIImageView = {
let i = UIImageView()
i.image = UIImage(named: "50x50.png")
return i
}()
let actorCommentLabel: UILabel = UILabel()
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(actionLabel)
addSubview(optionsLabel)
addSubview(posterImageView)
addSubview(posterNameLabel)
addSubview(posterHeadlineLabel)
addSubview(posterTimeLabel)
addSubview(posterCommentLabel)
addSubview(contentImageView)
addSubview(contentTitleLabel)
addSubview(contentDomainLabel)
addSubview(likeLabel)
addSubview(commentLabel)
addSubview(shareLabel)
addSubview(actorImageView)
addSubview(actorCommentLabel)
backgroundColor = UIColor.white
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setData(_ data: FeedItemData) {
actionLabel.text = data.actionText
posterNameLabel.text = data.posterName
posterNameLabel.sizeToFit()
posterHeadlineLabel.text = data.posterHeadline
posterHeadlineLabel.sizeToFit()
posterTimeLabel.text = data.posterTimestamp
posterTimeLabel.sizeToFit()
posterCommentLabel.text = data.posterComment
contentTitleLabel.text = data.contentTitle
contentDomainLabel.text = data.contentDomain
actorCommentLabel.text = data.actorComment
setNeedsLayout()
}
override func layoutSubviews() {
super.layoutSubviews()
let vMargin: CGFloat = 4
let spacing: CGFloat = 1
optionsLabel.frame = CGRect(x: bounds.width-optionsLabel.frame.width - hMargin, y: hMargin, width: optionsLabel.frame.width, height: optionsLabel.frame.height)
actionLabel.frame = CGRect(x: hMargin, y: hMargin, width: bounds.width-optionsLabel.frame.width, height: 0)
actionLabel.sizeToFit()
posterImageView.frame = CGRect(x: hMargin, y: actionLabel.frame.bottom + 10, width: posterImageView.frame.width, height: 0)
posterImageView.sizeToFit()
let contentInsets = UIEdgeInsets(top: -10, left: 2, bottom: 2, right: 3)
posterNameLabel.frame = CGRect(x: posterImageView.frame.right + contentInsets.left, y: posterImageView.frame.origin.y + contentInsets.top, width: posterNameLabel.frame.width, height: posterNameLabel.frame.height)
posterHeadlineLabel.frame = CGRect(x: posterImageView.frame.right + contentInsets.left, y: posterNameLabel.frame.bottom + spacing, width: posterHeadlineLabel.frame.width, height: posterHeadlineLabel.frame.height)
posterTimeLabel.frame = CGRect(x: posterImageView.frame.right + contentInsets.left, y: posterHeadlineLabel.frame.bottom + spacing, width: posterTimeLabel.frame.width, height: posterTimeLabel.frame.height)
posterCommentLabel.frame = CGRect(x: hMargin, y: max(posterImageView.frame.bottom, posterTimeLabel.frame.bottom + contentInsets.bottom), width: frame.width, height: 0)
posterCommentLabel.sizeToFit()
contentImageView.frame = CGRect(x: hMargin, y: posterCommentLabel.frame.bottom, width: frame.width, height: 0)
contentImageView.sizeToFit()
contentTitleLabel.frame = CGRect(x: hMargin, y: contentImageView.frame.bottom, width: frame.width, height: 0)
contentTitleLabel.sizeToFit()
contentDomainLabel.frame = CGRect(x: hMargin, y: contentTitleLabel.frame.bottom, width: frame.width, height: 0)
contentDomainLabel.sizeToFit()
likeLabel.frame = CGRect(x: hMargin, y: contentDomainLabel.frame.bottom + vMargin, width: 0, height: 0)
likeLabel.sizeToFit()
commentLabel.sizeToFit()
commentLabel.frame = CGRect(x: frame.width / 2 - commentLabel.frame.width / 2, y: contentDomainLabel.frame.bottom + vMargin, width: commentLabel.frame.width, height: commentLabel.frame.height)
shareLabel.sizeToFit()
shareLabel.frame = CGRect(x: frame.width - shareLabel.frame.width - hMargin, y: contentDomainLabel.frame.bottom + vMargin, width: shareLabel.frame.width, height: shareLabel.frame.height)
actorImageView.frame = CGRect(x: hMargin, y: likeLabel.frame.bottom + vMargin, width: 0, height: 0)
actorImageView.sizeToFit()
actorCommentLabel.frame = CGRect(x: actorImageView.frame.right + vMargin,
y: actorImageView.frame.minY + (actorImageView.frame.height - actorCommentLabel.frame.height) / 2,
width: frame.width-actorImageView.frame.width,
height: 0)
actorCommentLabel.sizeToFit()
}
override func sizeThatFits(_ size: CGSize) -> CGSize {
frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
layoutSubviews()
return CGSize(width: size.width, height: max(actorImageView.frame.bottom, actorCommentLabel.frame.bottom) + hMargin)
}
override var intrinsicContentSize: CGSize {
return sizeThatFits(CGSize(width: frame.width, height: CGFloat.greatestFiniteMagnitude))
}
}
extension CGRect {
var bottom: CGFloat {
return origin.y + size.height
}
var right: CGFloat {
return origin.x + size.width
}
}