| 1 | // |
| 2 | // CategoryCell.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 CategoryCell: UICollectionViewCell, JomloSelfConfiguringCell { |
| 13 | |
| 14 | private let iconLabel: UILabel = { |
| 15 | let label = UILabel() |
| 16 | label.font = .systemFont(ofSize: 32) |
| 17 | label.textAlignment = .center |
| 18 | return label |
| 19 | }() |
| 20 | |
| 21 | private let nameLabel: UILabel = { |
| 22 | let label = UILabel() |
| 23 | label.font = .preferredFont(forTextStyle: .caption1) |
| 24 | label.adjustsFontForContentSizeCategory = true |
| 25 | label.textColor = .white |
| 26 | label.textAlignment = .center |
| 27 | return label |
| 28 | }() |
| 29 | |
| 30 | override init(frame: CGRect) { |
| 31 | super.init(frame: frame) |
| 32 | setupViews() |
| 33 | } |
| 34 | |
| 35 | required init?(coder: NSCoder) { |
| 36 | fatalError("init(coder:) not supported") |
| 37 | } |
| 38 | |
| 39 | func configure(with item: CategoryItem) { |
| 40 | iconLabel.text = item.iconName |
| 41 | nameLabel.text = item.name |
| 42 | contentView.backgroundColor = UIColor(hex: item.colorHex) ?? .systemGray |
| 43 | } |
| 44 | |
| 45 | private func setupViews() { |
| 46 | contentView.layer.cornerRadius = 12 |
| 47 | contentView.clipsToBounds = true |
| 48 | |
| 49 | let stack = UIStackView(arrangedSubviews: [iconLabel, nameLabel]) |
| 50 | stack.axis = .vertical |
| 51 | stack.spacing = 4 |
| 52 | stack.alignment = .center |
| 53 | stack.translatesAutoresizingMaskIntoConstraints = false |
| 54 | |
| 55 | contentView.addSubview(stack) |
| 56 | NSLayoutConstraint.activate([ |
| 57 | stack.centerXAnchor.constraint(equalTo: contentView.centerXAnchor), |
| 58 | stack.centerYAnchor.constraint(equalTo: contentView.centerYAnchor), |
| 59 | ]) |
| 60 | |
| 61 | isAccessibilityElement = true |
| 62 | accessibilityTraits = .button |
| 63 | } |
| 64 | |
| 65 | override var accessibilityLabel: String? { |
| 66 | get { nameLabel.text } |
| 67 | set {} |
| 68 | } |
| 69 | } |