Created
September 25, 2019 05:59
-
-
Save wendyliga/cd95e2c364de28a6dde14615fbfb3309 to your computer and use it in GitHub Desktop.
TextureGram Header Node
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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