Files
language4english/LearnEnglish/LearnEnglish/Views/CategoryCardCell.swift
T

84 lines
3.7 KiB
Swift

import UIKit
final class CategoryCardCell: UITableViewCell {
private let cardView = UIView()
private let iconTile = UIView()
private let iconView = UIImageView()
private let nameLabel = UILabel()
private let countLabel = UILabel()
private let chevronView = UIImageView()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
backgroundColor = .clear
contentView.backgroundColor = .clear
selectionStyle = .none
cardView.backgroundColor = Theme.Color.cardBackground
cardView.layer.cornerRadius = Theme.Metrics.cardRadius
contentView.addSubview(cardView)
cardView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
cardView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 6),
cardView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 16),
cardView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -16),
cardView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -6)
])
iconTile.layer.cornerRadius = 12
iconView.contentMode = .scaleAspectFit
iconTile.addSubview(iconView)
iconTile.translatesAutoresizingMaskIntoConstraints = false
iconView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
iconTile.widthAnchor.constraint(equalToConstant: 44),
iconTile.heightAnchor.constraint(equalToConstant: 44),
iconView.centerXAnchor.constraint(equalTo: iconTile.centerXAnchor),
iconView.centerYAnchor.constraint(equalTo: iconTile.centerYAnchor)
])
nameLabel.font = .preferredFont(forTextStyle: .headline)
countLabel.font = .preferredFont(forTextStyle: .subheadline)
countLabel.textColor = .secondaryLabel
let textStack = UIStackView(arrangedSubviews: [nameLabel, countLabel])
textStack.axis = .vertical
textStack.spacing = 2
chevronView.image = UIImage(systemName: "chevron.right")
chevronView.tintColor = .tertiaryLabel
chevronView.contentMode = .scaleAspectFit
chevronView.setContentHuggingPriority(.required, for: .horizontal)
let row = UIStackView(arrangedSubviews: [iconTile, textStack, chevronView])
row.axis = .horizontal
row.alignment = .center
row.spacing = 12
cardView.addSubview(row)
row.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
row.topAnchor.constraint(equalTo: cardView.topAnchor, constant: 12),
row.leadingAnchor.constraint(equalTo: cardView.leadingAnchor, constant: 14),
row.trailingAnchor.constraint(equalTo: cardView.trailingAnchor, constant: -14),
row.bottomAnchor.constraint(equalTo: cardView.bottomAnchor, constant: -12)
])
}
@available(*, unavailable)
required init?(coder: NSCoder) { fatalError() }
func configure(with category: WordCategory, color: UIColor) {
nameLabel.text = category.name
countLabel.text = "\(category.words.count) 个单词"
iconView.image = UIImage(systemName: category.icon,
withConfiguration: UIImage.SymbolConfiguration(pointSize: 20, weight: .medium))
iconView.tintColor = color
iconTile.backgroundColor = color.withAlphaComponent(0.12)
}
override func setHighlighted(_ highlighted: Bool, animated: Bool) {
super.setHighlighted(highlighted, animated: animated)
cardView.setPressed(highlighted, animated: animated)
}
}