Skip to content

Instantly share code, notes, and snippets.

@wendyliga
Created September 25, 2019 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wendyliga/cd95e2c364de28a6dde14615fbfb3309 to your computer and use it in GitHub Desktop.
Save wendyliga/cd95e2c364de28a6dde14615fbfb3309 to your computer and use it in GitHub Desktop.
TextureGram Header Node
import AsyncDisplayKit
class HeaderNode: ASDisplayNode {
// MARK: - Values
private let post: Post
// MARK: - Nodes
private let profilePicture: ASImageNode
private let profileName: ASTextNode
private let postLocation: ASTextNode?
init(post: Post) {
self.post = post
profilePicture = ASImageNode()
profilePicture.image = post.user.userPhoto
profilePicture.style.preferredSize = CGSize(width: 32, height: 32)
profilePicture.cornerRadius = 32/2
profileName = ASTextNode()
profileName.attributedText = NSAttributedString.bold(post.user.username)
if let location = post.location {
postLocation = ASTextNode()
postLocation?.attributedText = NSAttributedString.subtitle(location)
}else{
postLocation = nil
}
super.init()
self.automaticallyManagesSubnodes = true
}
override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec {
var leftStackComponents = [ASLayoutElement]()
leftStackComponents.append(profileName)
if let postLocation = self.postLocation {
leftStackComponents.append(postLocation)
}
let leftStack = ASStackLayoutSpec(direction: .vertical,
spacing: 0,
justifyContent: .start,
alignItems: .start,
children: leftStackComponents)
let mainStack = ASStackLayoutSpec(direction: .horizontal,
spacing: 6,
justifyContent: .start,
alignItems: .center,
children: [profilePicture, leftStack])
let padding = ASInsetLayoutSpec(insets: UIEdgeInsets(top: 8, left: 8, bottom: 4, right: 8), child: mainStack)
return padding
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment