Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. import AsyncDisplayKit
  2.  
  3. class PostCell: ASCellNode {
  4. let post: Post
  5.  
  6. // MARK: - Nodes
  7.  
  8. let headerNode: HeaderNode
  9. let postImage: ASImageNode
  10. let actionNode: ActionNode
  11. let postLikes: ASTextNode
  12. let postDescription: ASTextNode
  13.  
  14. init(post: Post) {
  15. self.post = post
  16. headerNode = HeaderNode(post: post)
  17.  
  18. postImage = ASImageNode()
  19. postImage.contentMode = .scaleAspectFit
  20. postImage.style.width = ASDimension(unit: .fraction, value: 1)
  21. postImage.image = post.image
  22.  
  23. actionNode = ActionNode()
  24.  
  25. postLikes = ASTextNode()
  26. postLikes.attributedText = NSAttributedString.bold("\(post.likeCount) Likes")
  27.  
  28. postDescription = ASTextNode()
  29. postDescription.maximumNumberOfLines = 0
  30. postDescription.attributedText = NSAttributedString.normal(post.description)
  31.  
  32. super.init()
  33.  
  34. self.automaticallyManagesSubnodes = true
  35. }
  36.  
  37. override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec {
  38.  
  39. let postImageWithRatio = ASRatioLayoutSpec(ratio: 1/1.5, child: postImage)
  40.  
  41. let postLikesWithPadding = ASInsetLayoutSpec(insets: UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 8), child: self.postLikes)
  42.  
  43. let postDescriptionWithPadding = ASInsetLayoutSpec(insets: UIEdgeInsets(top: 4, left: 8, bottom: 0, right: 8), child: self.postDescription)
  44.  
  45. let mainStack = ASStackLayoutSpec(direction: .vertical,
  46. spacing: 0,
  47. justifyContent: .start,
  48. alignItems: .start,
  49. children: [headerNode,
  50. postImageWithRatio,
  51. actionNode,
  52. postLikesWithPadding,
  53. postDescriptionWithPadding])
  54.  
  55. return mainStack
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement