forked from TextureGroup/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLayoutExampleNode.swift
More file actions
282 lines (216 loc) · 9.5 KB
/
Copy pathLayoutExampleNode.swift
File metadata and controls
282 lines (216 loc) · 9.5 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//
// LayoutExampleNode.swift
// Sample
//
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
import AsyncDisplayKit
class LayoutExampleNode: ASDisplayNode {
override required init() {
super.init()
automaticallyManagesSubnodes = true
backgroundColor = .white
}
class func title() -> String {
assertionFailure("All layout example nodes must provide a title!")
return ""
}
class func descriptionTitle() -> String? {
return nil
}
}
class HeaderWithRightAndLeftItems : LayoutExampleNode {
let userNameNode = ASTextNode()
let postLocationNode = ASTextNode()
let postTimeNode = ASTextNode()
required init() {
super.init()
userNameNode.attributedText = NSAttributedString.attributedString(string: "hannahmbanana", fontSize: 20, color: .darkBlueColor())
userNameNode.maximumNumberOfLines = 1
userNameNode.truncationMode = .byTruncatingTail
postLocationNode.attributedText = NSAttributedString.attributedString(string: "Sunset Beach, San Fransisco, CA", fontSize: 20, color: .lightBlueColor())
postLocationNode.maximumNumberOfLines = 1
postLocationNode.truncationMode = .byTruncatingTail
postTimeNode.attributedText = NSAttributedString.attributedString(string: "30m", fontSize: 20, color: .lightGray)
postTimeNode.maximumNumberOfLines = 1
postTimeNode.truncationMode = .byTruncatingTail
}
override class func title() -> String {
return "Header with left and right justified text"
}
override class func descriptionTitle() -> String? {
return "try rotating me!"
}
}
class PhotoWithInsetTextOverlay : LayoutExampleNode {
let photoNode = ASNetworkImageNode()
let titleNode = ASTextNode()
required init() {
super.init()
backgroundColor = .clear
photoNode.url = URL(string: "http://texturegroup.org/static/images/layout-examples-photo-with-inset-text-overlay-photo.png")
photoNode.willDisplayNodeContentWithRenderingContext = { context, drawParameters in
let bounds = context.boundingBoxOfClipPath
UIBezierPath(roundedRect: bounds, cornerRadius: 10).addClip()
}
titleNode.attributedText = NSAttributedString.attributedString(string: "family fall hikes", fontSize: 16, color: .white)
titleNode.truncationAttributedText = NSAttributedString.attributedString(string: "...", fontSize: 16, color: .white)
titleNode.maximumNumberOfLines = 2
titleNode.truncationMode = .byTruncatingTail
}
override class func title() -> String {
return "Photo with inset text overlay"
}
override class func descriptionTitle() -> String? {
return "try rotating me!"
}
}
class PhotoWithOutsetIconOverlay : LayoutExampleNode {
let photoNode = ASNetworkImageNode()
let iconNode = ASNetworkImageNode()
required init() {
super.init()
photoNode.url = URL(string: "http://texturegroup.org/static/images/layout-examples-photo-with-outset-icon-overlay-photo.png")
iconNode.url = URL(string: "http://texturegroup.org/static/images/layout-examples-photo-with-outset-icon-overlay-icon.png")
iconNode.imageModificationBlock = { image in
let profileImageSize = CGSize(width: 60, height: 60)
return image.makeCircularImage(size: profileImageSize, borderWidth: 10)
}
}
override class func title() -> String {
return "Photo with outset icon overlay"
}
override class func descriptionTitle() -> String? {
return nil
}
}
class FlexibleSeparatorSurroundingContent : LayoutExampleNode {
let topSeparator = ASImageNode()
let bottomSeparator = ASImageNode()
let textNode = ASTextNode()
required init() {
super.init()
topSeparator.image = UIImage.as_resizableRoundedImage(withCornerRadius: 1.0, cornerColor: .black, fill: .black)
textNode.attributedText = NSAttributedString.attributedString(string: "this is a long text node", fontSize: 16, color: .black)
bottomSeparator.image = UIImage.as_resizableRoundedImage(withCornerRadius: 1.0, cornerColor: .black, fill: .black)
}
override class func title() -> String {
return "Top and bottom cell separator lines"
}
override class func descriptionTitle() -> String? {
return "try rotating me!"
}
}
class CornerLayoutSample : PhotoWithOutsetIconOverlay {
let photoNode1 = ASImageNode()
let photoNode2 = ASImageNode()
let dotNode = ASImageNode()
let badgeTextNode = ASTextNode()
let badgeImageNode = ASImageNode()
struct ImageSize {
static let avatar = CGSize(width: 100, height: 100)
static let icon = CGSize(width: 26, height: 26)
}
struct ImageColor {
static let avatar = UIColor.lightGray
static let icon = UIColor.red
}
required init() {
super.init()
let avatarImage = UIImage.draw(size: ImageSize.avatar, fillColor: ImageColor.avatar) { () -> UIBezierPath in
return UIBezierPath(roundedRect: CGRect(origin: CGPoint.zero, size: ImageSize.avatar), cornerRadius: ImageSize.avatar.width / 20)
}
let iconImage = UIImage.draw(size: ImageSize.icon, fillColor: ImageColor.icon) { () -> UIBezierPath in
return UIBezierPath(ovalIn: CGRect(origin: CGPoint.zero, size: ImageSize.icon))
}
photoNode1.image = avatarImage
photoNode2.image = avatarImage
dotNode.image = iconImage
badgeTextNode.attributedText = NSAttributedString.attributedString(string: " 999+ ", fontSize: 20, color: .white)
badgeImageNode.image = UIImage.as_resizableRoundedImage(withCornerRadius: 12, cornerColor: .clear, fill: .red)
}
override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec {
photoNode.style.preferredSize = ImageSize.avatar
iconNode.style.preferredSize = ImageSize.icon
let badgeSpec = ASBackgroundLayoutSpec(child: badgeTextNode, background: badgeImageNode)
let cornerSpec1 = ASCornerLayoutSpec(child: photoNode1, corner: dotNode, location: .topRight)
let cornerSpec2 = ASCornerLayoutSpec(child: photoNode2, corner: badgeSpec, location: .topRight)
let cornerSpec3 = ASCornerLayoutSpec(child: photoNode, corner: iconNode, location: .topRight)
cornerSpec1.offset = CGPoint(x: -3, y: 3)
let stackSpec = ASStackLayoutSpec.vertical()
stackSpec.spacing = 40
stackSpec.children = [cornerSpec1, cornerSpec2, cornerSpec3]
return stackSpec
}
override class func title() -> String {
return "Declarative way for Corner image Layout"
}
override class func descriptionTitle() -> String? {
return nil
}
}
class UserProfileSample : LayoutExampleNode {
let badgeNode = ASImageNode()
let avatarNode = ASImageNode()
let usernameNode = ASTextNode()
let subtitleNode = ASTextNode()
struct ImageSize {
static let avatar = CGSize(width: 44, height: 44)
static let badge = CGSize(width: 15, height: 15)
}
struct ImageColor {
static let avatar = UIColor.lightGray
static let badge = UIColor.red
}
required init() {
super.init()
avatarNode.image = UIImage.draw(size: ImageSize.avatar, fillColor: ImageColor.avatar) { () -> UIBezierPath in
return UIBezierPath(ovalIn: CGRect(origin: CGPoint.zero, size: ImageSize.avatar))
}
badgeNode.image = UIImage.draw(size: ImageSize.badge, fillColor: ImageColor.badge) { () -> UIBezierPath in
return UIBezierPath(ovalIn: CGRect(origin: CGPoint.zero, size: ImageSize.badge))
}
makeSingleLine(for: usernameNode, with: "Hello world", fontSize: 17, textColor: .black)
makeSingleLine(for: subtitleNode, with: "This is a long long subtitle, with a long long appended string.", fontSize: 14, textColor: .lightGray)
}
private func makeSingleLine(for node: ASTextNode, with text: String, fontSize: CGFloat, textColor: UIColor) {
node.attributedText = NSAttributedString.attributedString(string: text, fontSize: fontSize, color: textColor)
node.maximumNumberOfLines = 1
}
override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec {
let avatarBox = ASCornerLayoutSpec(child: avatarNode, corner: badgeNode, location: .bottomRight)
avatarBox.offset = CGPoint(x: -6, y: -6)
let textBox = ASStackLayoutSpec.vertical()
textBox.justifyContent = .spaceAround
textBox.children = [usernameNode, subtitleNode]
let profileBox = ASStackLayoutSpec.horizontal()
profileBox.spacing = 10
profileBox.children = [avatarBox, textBox]
// Apply text truncation
let elems: [ASLayoutElement] = [usernameNode, subtitleNode, textBox, profileBox]
for elem in elems {
elem.style.flexShrink = 1
}
let insetBox = ASInsetLayoutSpec(
insets: UIEdgeInsets(top: 120, left: 20, bottom: CGFloat.infinity, right: 20),
child: profileBox
)
return insetBox
}
override class func title() -> String {
return "Common user profile layout."
}
override class func descriptionTitle() -> String? {
return "For corner image layout and text truncation."
}
}