@@ -16,14 +16,14 @@ class CardView: UIView {
1616 update()
1717 }
1818 }
19− var width: CGFloat = 300 {
19+ var minWidth: CGFloat = 300 {
2020 didSet {
21− update()
21+// update()
2222 }
2323 }
2424 var height: CGFloat = 400 {
2525 didSet {
26− update()
26+// update()
2727 }
2828 }
2929
@@ -31,17 +31,32 @@ class CardView: UIView {
3131 super.init(frame: frame)
3232 layer.masksToBounds = true
3333 layer.cornerRadius = 20
34− backgroundColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)
34+ gradient.colors = [#colorLiteral(red: 0.9841352105, green: 0.9841352105, blue: 0.9841352105, alpha: 1).cgColor, #colorLiteral(red: 0.9456828237, green: 0.9456828237, blue: 0.9456828237, alpha: 1).cgColor]
35+ gradient.startPoint = CGPoint(x: 0.5, y: 0)
36+ gradient.endPoint = CGPoint(x: 0.5, y: 1)
37+ layer.addSublayer(gradient)
38+// backgroundColor = #colorLiteral(red: 0.9638966278, green: 0.9734401588, blue: 0.9734401588, alpha: 1)
3539 }
3640
3741 required init?(coder aDecoder: NSCoder) {
3842 fatalError("init(coder:) has not been implemented")
3943 }
44+
45+ private let gradient = CAGradientLayer()
46+
4047
4148 private let padding = CGPoint(x: 32, y: 32)
4249 private let vPadding1: CGFloat = 16
4350 private let vPadding2: CGFloat = 16
44− private let minTextHeight: CGFloat = 50
51+ private var minTextHeight: CGFloat {
52+ return CGFloat(height * 0.2)
53+ }
54+ private let gradientRatio: CGFloat = 0.2
55+ private let minGradientAmount: CGFloat = 70
56+ private let hiddenByGradient: CGFloat = 10
57+ private var maxAttachmentDim: CGFloat {
58+ return minWidth / 2.3
59+ }
4560
4661// private let avatar = UIImage()
4762// private let name = UIImage()
@@ -54,10 +69,9 @@ class CardView: UIView {
5469 addSubview(largeStack)
5570
5671 let userView = StackView(axis: .horizontal, alignment: .center)
57−// userView.backgroundColor = #colorLiteral(red: 0.5568627715, green: 0.3529411852, blue: 0.9686274529, alpha: 1)
5872
5973
60− let avatarDim: CGFloat = 50
74+ let avatarDim: CGFloat = 64
6175 let avatar = UIImageView()
6276 post?.owner?.avatar?.load(into: avatar)
6377 avatar.layer.cornerRadius = avatarDim/2
@@ -74,6 +88,7 @@ class CardView: UIView {
7488 let lastName = post?.owner?.lastName ?? ""
7589 userName.text = "\(firstName) \(lastName)"
7690 userName.font = UIFont.systemFont(ofSize: 28, weight: UIFontWeightHeavy)
91+ userName.textColor = UIColor.darkText
7792 userName.sizeToFit()
7893 userView.addArrangedSubview(.view(userName))
7994
@@ -84,12 +99,16 @@ class CardView: UIView {
8499 largeStack.addArrangedSubview(.spacer(vPadding1))
85100
86101
102+ let width = max(minWidth - 2 * padding.x, userView.frame.width)
103+
104+
87105 let photoView = CollectionView()
88106 photoView.alignment = .top
89107 photoView.horizontalPadding = 8
90108 photoView.verticalPadding = 8
91109 photoView.padding = CGPoint.zero
92− var maxPhotoDim: CGFloat = 150
110+ photoView.maxWidth = width
111+ var maxPhotoDim: CGFloat = maxAttachmentDim
93112 if let attachments = post?.attachments {
94113 for attachment in attachments {
95114 if let v = attachment.value {
@@ -98,6 +117,8 @@ class CardView: UIView {
98117 if var pWidth = photo.width.value >>> {CGFloat($0)},
99118 var pHeight = photo.height.value >>> {CGFloat($0)} {
100119 let photoV = UIImageView()
120+ photoV.layer.minificationFilter = kCAFilterTrilinear
121+ photoV.contentMode = .scaleAspectFill
101122 photo.image?.load(into: photoV)
102123 var k: CGFloat!
103124 if pWidth > pHeight {
@@ -120,7 +141,7 @@ class CardView: UIView {
120141 photoView.layoutIfNeeded()
121142
122143 var textHeight = height
123− textHeight -= padding.y + vPadding1 + userView.frame.height
144+ textHeight -= padding.y * 2 + vPadding1 + userView.frame.height
124145 if let count = post?.attachments.count{
125146 if count > 0 {
126147 textHeight -= vPadding2 + photoView.frame.height
@@ -132,12 +153,28 @@ class CardView: UIView {
132153
133154 let body = UILabel()
134155 body.numberOfLines = 0
135− let bodyWidth: Double = Double(width - padding.x * 2)
156+ let bodyWidth: Double = Double(width)
136157 body.font = UIFont.systemFont(ofSize: 22, weight: UIFontWeightSemibold)
137158 body.text = post?.body
159+ body.textColor = UIColor.darkText
138160 let size = body.textRect(forBounds: CGRect(x: 0, y: 0, width: bodyWidth, height: Double.infinity), limitedToNumberOfLines: 0).size
139− body.frame.size.height = min(size.height, textHeight)
140161 body.frame.size.width = size.width
162+ if size.height > textHeight {
163+ body.frame.size.height = textHeight
164+ let maskView = UIView()
165+ let maskLayer = CAGradientLayer()
166+ maskLayer.colors = [UIColor.black.cgColor, UIColor.clear.cgColor]
167+ maskLayer.endPoint = CGPoint(x: 0.5, y: 1 - (hiddenByGradient/textHeight))
168+ let minPoint = minGradientAmount / textHeight
169+ maskLayer.startPoint = CGPoint(x: 0.5, y: 1 - max(minPoint, gradientRatio))
170+ print(maskLayer.startPoint)
171+ maskLayer.frame = body.bounds
172+ maskView.frame = body.bounds
173+ maskView.layer.addSublayer(maskLayer)
174+ body.mask = maskView
175+ } else {
176+ body.frame.size.height = size.height
177+ }
141178
142179 largeStack.addArrangedSubview(.view(body))
143180
@@ -148,7 +185,12 @@ class CardView: UIView {
148185 }
149186 }
150187
151− frame.size = CGSize(width: width, height: height)
188+ largeStack.layoutIfNeeded()
189+
190+ frame.size = CGSize(width: width + padding.x * 2, height: min(height, largeStack.frame.height))
191+
192+ gradient.frame = self.bounds
193+ gradient.endPoint = CGPoint(x: 0.5, y: bounds.height / height)
152194
153195 }
154196}