Skip to content

Commit e6be1bf

Browse files
committed
Add a view that layouts with NotAutoLayout
1 parent 82d4a0a commit e6be1bf

2 files changed

Lines changed: 241 additions & 0 deletions

File tree

LayoutFrameworkBenchmark.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
24661D001F4EFFF5002CB883 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24661CFF1F4EFFF5002CB883 /* AppDelegate.swift */; };
2525
24661D071F4EFFF5002CB883 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 24661D061F4EFFF5002CB883 /* Assets.xcassets */; };
2626
24661D0A1F4EFFF5002CB883 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 24661D081F4EFFF5002CB883 /* LaunchScreen.storyboard */; };
27+
BF3DC69820B560A400536177 /* FeedItemNotAutoLayoutView.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF3DC69720B560A400536177 /* FeedItemNotAutoLayoutView.swift */; };
2728
/* End PBXBuildFile section */
2829

2930
/* Begin PBXFileReference section */
@@ -48,6 +49,7 @@
4849
24661D0B1F4EFFF5002CB883 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
4950
4F34489CA12B845548D7F17E /* Pods_LayoutFrameworkBenchmark.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_LayoutFrameworkBenchmark.framework; sourceTree = BUILT_PRODUCTS_DIR; };
5051
73BD901DE3512A23A7603899 /* Pods-LayoutFrameworkBenchmark.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-LayoutFrameworkBenchmark.debug.xcconfig"; path = "Pods/Target Support Files/Pods-LayoutFrameworkBenchmark/Pods-LayoutFrameworkBenchmark.debug.xcconfig"; sourceTree = "<group>"; };
52+
BF3DC69720B560A400536177 /* FeedItemNotAutoLayoutView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeedItemNotAutoLayoutView.swift; sourceTree = "<group>"; };
5153
/* End PBXFileReference section */
5254

5355
/* Begin PBXFrameworksBuildPhase section */
@@ -69,6 +71,7 @@
6971
2401BC921F4F020D00788998 /* FlexLayout */,
7072
2401BC8D1F4F01CC00788998 /* LayoutKit */,
7173
2401BC9C1F4F041400788998 /* ManualLayout */,
74+
BF3DC69620B5608000536177 /* NotAutoLayout */,
7275
2401BC911F4F020600788998 /* PinLayout */,
7376
2401BC9F1F4F043800788998 /* UIStackView */,
7477
2401BC751F4F018C00788998 /* BenchmarkViewController.swift */,
@@ -185,6 +188,14 @@
185188
name = Frameworks;
186189
sourceTree = "<group>";
187190
};
191+
BF3DC69620B5608000536177 /* NotAutoLayout */ = {
192+
isa = PBXGroup;
193+
children = (
194+
BF3DC69720B560A400536177 /* FeedItemNotAutoLayoutView.swift */,
195+
);
196+
name = NotAutoLayout;
197+
sourceTree = "<group>";
198+
};
188199
/* End PBXGroup section */
189200

190201
/* Begin PBXNativeTarget section */
@@ -315,6 +326,7 @@
315326
2401BC821F4F018C00788998 /* CollectionViewController.swift in Sources */,
316327
2401BC9E1F4F042700788998 /* FeedItemManualView.swift in Sources */,
317328
2401BC811F4F018C00788998 /* BenchmarkViewController.swift in Sources */,
329+
BF3DC69820B560A400536177 /* FeedItemNotAutoLayoutView.swift in Sources */,
318330
2401BC9B1F4F03B300788998 /* ProfileCardLayout.swift in Sources */,
319331
2401BC941F4F021F00788998 /* FeedItemFlexLayoutView.swift in Sources */,
320332
2401BCA41F4F045F00788998 /* FeedItemAutoLayoutView.swift in Sources */,
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
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

Comments
 (0)