|
| 1 | +// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. |
| 2 | +// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 3 | +// |
| 4 | +// Unless required by applicable law or agreed to in writing, |
| 5 | +// software distributed under the License is distributed on an "AS IS" BASIS, |
| 6 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 7 | + |
| 8 | +import Foundation |
| 9 | +import NotAutoLayout |
| 10 | + |
| 11 | +/// A LinkedIn feed item that is implemented with NotAutoLayout code. |
| 12 | +class FeedItemNotAutoLayoutView: UIView, DataBinder { |
| 13 | + |
| 14 | + let actionLabel: UILabel = { |
| 15 | + let l = UILabel() |
| 16 | + l.backgroundColor = UIColor.blue |
| 17 | + return l |
| 18 | + }() |
| 19 | + |
| 20 | + let optionsLabel: UILabel = { |
| 21 | + let l = UILabel() |
| 22 | + l.text = "..." |
| 23 | + l.sizeToFit() |
| 24 | + return l |
| 25 | + }() |
| 26 | + |
| 27 | + let posterImageView: UIImageView = { |
| 28 | + let i = UIImageView() |
| 29 | + i.image = UIImage(named: "50x50.png") |
| 30 | + i.backgroundColor = UIColor.orange |
| 31 | + i.contentMode = .scaleToFill |
| 32 | + i.sizeToFit() |
| 33 | + return i |
| 34 | + }() |
| 35 | + |
| 36 | + let posterNameLabel: UILabel = UILabel() |
| 37 | + |
| 38 | + let posterHeadlineLabel: UILabel = { |
| 39 | + let l = UILabel() |
| 40 | + l.numberOfLines = 3 |
| 41 | + return l |
| 42 | + }() |
| 43 | + |
| 44 | + let posterTimeLabel: UILabel = UILabel() |
| 45 | + let posterCommentLabel: UILabel = UILabel() |
| 46 | + |
| 47 | + let contentImageView: UIImageView = { |
| 48 | + let i = UIImageView() |
| 49 | + i.image = UIImage(named: "350x200.png") |
| 50 | + i.contentMode = .scaleToFill |
| 51 | + i.sizeToFit() |
| 52 | + return i |
| 53 | + }() |
| 54 | + |
| 55 | + let contentTitleLabel: UILabel = UILabel() |
| 56 | + let contentDomainLabel: UILabel = UILabel() |
| 57 | + |
| 58 | + let likeLabel: UILabel = { |
| 59 | + let l = UILabel() |
| 60 | + l.backgroundColor = UIColor(red: 0, green: 0.9, blue: 0, alpha: 1) |
| 61 | + l.text = "Like" |
| 62 | + return l |
| 63 | + }() |
| 64 | + |
| 65 | + let commentLabel: UILabel = { |
| 66 | + let l = UILabel() |
| 67 | + l.text = "Comment" |
| 68 | + l.backgroundColor = UIColor(red: 0, green: 1.0, blue: 0, alpha: 1) |
| 69 | + l.textAlignment = .center |
| 70 | + return l |
| 71 | + }() |
| 72 | + |
| 73 | + let shareLabel: UILabel = { |
| 74 | + let l = UILabel() |
| 75 | + l.text = "Share" |
| 76 | + l.backgroundColor = UIColor(red: 0, green: 0.8, blue: 0, alpha: 1) |
| 77 | + l.textAlignment = .right |
| 78 | + return l |
| 79 | + }() |
| 80 | + |
| 81 | + let actorImageView: UIImageView = { |
| 82 | + let i = UIImageView() |
| 83 | + i.image = UIImage(named: "50x50.png") |
| 84 | + return i |
| 85 | + }() |
| 86 | + |
| 87 | + let actorCommentLabel: UILabel = UILabel() |
| 88 | + |
| 89 | + override init(frame: CGRect) { |
| 90 | + super.init(frame: frame) |
| 91 | + addSubview(actionLabel) |
| 92 | + addSubview(optionsLabel) |
| 93 | + addSubview(posterImageView) |
| 94 | + addSubview(posterNameLabel) |
| 95 | + addSubview(posterHeadlineLabel) |
| 96 | + addSubview(posterTimeLabel) |
| 97 | + addSubview(posterCommentLabel) |
| 98 | + addSubview(contentImageView) |
| 99 | + addSubview(contentTitleLabel) |
| 100 | + addSubview(contentDomainLabel) |
| 101 | + addSubview(likeLabel) |
| 102 | + addSubview(commentLabel) |
| 103 | + addSubview(shareLabel) |
| 104 | + addSubview(actorImageView) |
| 105 | + addSubview(actorCommentLabel) |
| 106 | + backgroundColor = UIColor.white |
| 107 | + } |
| 108 | + |
| 109 | + required init?(coder aDecoder: NSCoder) { |
| 110 | + fatalError("init(coder:) has not been implemented") |
| 111 | + } |
| 112 | + |
| 113 | + func setData(_ data: FeedItemData) { |
| 114 | + actionLabel.text = data.actionText |
| 115 | + posterNameLabel.text = data.posterName |
| 116 | + posterHeadlineLabel.text = data.posterHeadline |
| 117 | + posterTimeLabel.text = data.posterTimestamp |
| 118 | + posterCommentLabel.text = data.posterComment |
| 119 | + contentTitleLabel.text = data.contentTitle |
| 120 | + contentDomainLabel.text = data.contentDomain |
| 121 | + actorCommentLabel.text = data.actorComment |
| 122 | + setNeedsLayout() |
| 123 | + } |
| 124 | + |
| 125 | + override func layoutSubviews() { |
| 126 | + super.layoutSubviews() |
| 127 | + |
| 128 | + nal.layout(optionsLabel, by: { $0 |
| 129 | + .setTopRight(by: { $0.topRight }) |
| 130 | + .fitSize() |
| 131 | + }) |
| 132 | + nal.layout(actionLabel, by: { $0 |
| 133 | + .setTopLeft(by: { $0.topLeft }) |
| 134 | + .fitSize() |
| 135 | + .pinchingRight(to: { (frame, _) in |
| 136 | + NotAutoLayout.Float(min(frame.right.cgValue, self.optionsLabel.frame.minX)) |
| 137 | + }) |
| 138 | + }) |
| 139 | + |
| 140 | + nal.layout(posterImageView, by: { $0 |
| 141 | + .pinTopLeft(to: actionLabel, with: { $0.bottomLeft }) |
| 142 | + .fitSize() |
| 143 | + }) |
| 144 | + nal.layout(posterNameLabel, by: { $0 |
| 145 | + .pinTopLeft(to: posterImageView, with: { $0.topRight + .init(x: 1, y: 0) }) |
| 146 | + .setRight(by: { $0.right - 3 }) |
| 147 | + .fitHeight() |
| 148 | + }) |
| 149 | + |
| 150 | + nal.layout(posterHeadlineLabel) { $0 |
| 151 | + .pinLeft(to: posterImageView, with: { $0.right + 1 }) |
| 152 | + .setRight(by: { $0.right - 3 }) |
| 153 | + .pinTop(to: posterNameLabel, with: { $0.bottom + 1 }) |
| 154 | + .fitHeight() |
| 155 | + } |
| 156 | + |
| 157 | + nal.layout(posterTimeLabel, by: { $0 |
| 158 | + .pinLeft(to: posterImageView, with: { $0.right + 1 }) |
| 159 | + .setRight(by: { $0.right - 3 }) |
| 160 | + .pinTop(to: posterHeadlineLabel, with: { $0.bottom + 1 }) |
| 161 | + .fitHeight() |
| 162 | + }) |
| 163 | + |
| 164 | + nal.layout(posterCommentLabel, by: { $0 |
| 165 | + .setCenter(by: { $0.center }) |
| 166 | + .setTop(by: { _ in NotAutoLayout.Float(max(self.posterImageView.frame.maxY, self.posterTimeLabel.frame.maxY + 2)) }) |
| 167 | + .setWidth(by: { $0.width }) |
| 168 | + .fitHeight() |
| 169 | + }) |
| 170 | + |
| 171 | + nal.layout(contentImageView, by: { $0 |
| 172 | + .setCenter(by: { $0.center }) |
| 173 | + .pinTop(to: posterCommentLabel, with: { $0.bottom }) |
| 174 | + .fitSize() |
| 175 | + }) |
| 176 | + |
| 177 | + nal.layout(contentTitleLabel, by: { $0 |
| 178 | + .setCenter(by: { $0.center }) |
| 179 | + .pinTop(to: contentImageView, with: { $0.bottom }) |
| 180 | + .setWidth(by: { $0.width }) |
| 181 | + .fitHeight() |
| 182 | + }) |
| 183 | + |
| 184 | + nal.layout(contentDomainLabel, by: { $0 |
| 185 | + .setCenter(by: { $0.center }) |
| 186 | + .pinTop(to: contentTitleLabel, with: { $0.bottom }) |
| 187 | + .setWidth(by: { $0.width }) |
| 188 | + .fitHeight() |
| 189 | + }) |
| 190 | + |
| 191 | + nal.layout(likeLabel, by: { $0 |
| 192 | + .pinTopLeft(to: contentDomainLabel, with: { $0.bottomLeft }) |
| 193 | + .fitSize() |
| 194 | + }) |
| 195 | + |
| 196 | + nal.layout(commentLabel, by: { $0 |
| 197 | + .pinTopCenter(to: contentDomainLabel, with: { $0.bottomCenter }) |
| 198 | + .fitSize() |
| 199 | + }) |
| 200 | + |
| 201 | + nal.layout(shareLabel, by: { $0 |
| 202 | + .pinTopRight(to: contentDomainLabel, with: { $0.bottomRight }) |
| 203 | + .fitSize() |
| 204 | + }) |
| 205 | + |
| 206 | + nal.layout(actorImageView, by: { $0 |
| 207 | + .pinTopLeft(to: likeLabel, with: { $0.bottomLeft }) |
| 208 | + .fitSize() |
| 209 | + }) |
| 210 | + |
| 211 | + nal.layout(actorCommentLabel, by: { $0 |
| 212 | + .pinTopLeft(to: actorImageView, with: { $0.topRight }) |
| 213 | + .setRight(by: { $0.right }) |
| 214 | + .fitHeight() |
| 215 | + }) |
| 216 | + |
| 217 | + } |
| 218 | + |
| 219 | + override func sizeThatFits(_ size: CGSize) -> CGSize { |
| 220 | + frame = CGRect(x: 0, y: 0, width: size.width, height: size.height) |
| 221 | + layoutSubviews() |
| 222 | + return CGSize(width: size.width, height: max(actorImageView.frame.bottom, actorCommentLabel.frame.bottom)) |
| 223 | + } |
| 224 | + |
| 225 | + override var intrinsicContentSize: CGSize { |
| 226 | + return sizeThatFits(CGSize(width: frame.width, height: CGFloat.greatestFiniteMagnitude)) |
| 227 | + } |
| 228 | + |
| 229 | +} |
0 commit comments