main
swift 96 lines 3.06 KB
Raw
1 //
2 // HeroCell.swift
3 // JomloCollectionView Example
4 //
5 // Created by Seto Elkahfi on 2026-01-27.
6 // Copyright © 2026 Seto Elkahfi. All rights reserved.
7 //
8
9 import JomloCollectionView
10 import UIKit
11
12 final class HeroCell: UICollectionViewCell, JomloSelfConfiguringCell {
13
14 private let titleLabel: UILabel = {
15 let label = UILabel()
16 label.font = .systemFont(ofSize: 28, weight: .bold)
17 label.textColor = .white
18 label.numberOfLines = 2
19 return label
20 }()
21
22 private let subtitleLabel: UILabel = {
23 let label = UILabel()
24 label.font = .systemFont(ofSize: 16, weight: .medium)
25 label.textColor = .white.withAlphaComponent(0.8)
26 return label
27 }()
28
29 private let playButton: UIButton = {
30 let button = UIButton(type: .system)
31 button.setTitle("\u{25B6} Play", for: .normal)
32 button.titleLabel?.font = .systemFont(ofSize: 16, weight: .semibold)
33 button.setTitleColor(.black, for: .normal)
34 button.backgroundColor = .white
35 button.layer.cornerRadius = 8
36 button.isUserInteractionEnabled = false
37 return button
38 }()
39
40 private let gradientLayer = CAGradientLayer()
41
42 override init(frame: CGRect) {
43 super.init(frame: frame)
44 setupViews()
45 }
46
47 required init?(coder: NSCoder) {
48 fatalError("init(coder:) not supported")
49 }
50
51 override func layoutSubviews() {
52 super.layoutSubviews()
53 gradientLayer.frame = contentView.bounds
54 }
55
56 func configure(with item: HeroItem) {
57 titleLabel.text = item.title
58 subtitleLabel.text = item.subtitle
59
60 let topColor = UIColor(hex: item.gradientColors.top) ?? .systemBlue
61 let bottomColor = UIColor(hex: item.gradientColors.bottom) ?? .systemIndigo
62 gradientLayer.colors = [topColor.cgColor, bottomColor.cgColor]
63 }
64
65 private func setupViews() {
66 contentView.layer.cornerRadius = 16
67 contentView.clipsToBounds = true
68 contentView.layer.insertSublayer(gradientLayer, at: 0)
69 gradientLayer.startPoint = CGPoint(x: 0, y: 0)
70 gradientLayer.endPoint = CGPoint(x: 1, y: 1)
71
72 let stack = UIStackView(arrangedSubviews: [titleLabel, subtitleLabel, playButton])
73 stack.axis = .vertical
74 stack.spacing = 8
75 stack.alignment = .leading
76 stack.translatesAutoresizingMaskIntoConstraints = false
77
78 contentView.addSubview(stack)
79 NSLayoutConstraint.activate([
80 stack.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 20),
81 stack.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -20),
82 stack.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -24),
83 playButton.widthAnchor.constraint(equalToConstant: 100),
84 playButton.heightAnchor.constraint(equalToConstant: 36),
85 ])
86
87 // Accessibility
88 isAccessibilityElement = true
89 accessibilityTraits = .button
90 }
91
92 override var accessibilityLabel: String? {
93 get { titleLabel.text }
94 set {}
95 }
96 }