|
| 1 | +// |
| 2 | +// BitnagilButtonTableViewCell.swift |
| 3 | +// Presentation |
| 4 | +// |
| 5 | +// Created by 이동현 on 7/30/25. |
| 6 | +// |
| 7 | + |
| 8 | +import SnapKit |
| 9 | +import UIKit |
| 10 | + |
| 11 | +protocol BitnagilButtonTableViewCellDelegate: AnyObject { |
| 12 | + func bitnagilButtonTableViewCellDidTapButton(_ sender: BitnagilButtonTableViewCell) |
| 13 | +} |
| 14 | + |
| 15 | +final class BitnagilButtonTableViewCell: BitnagilBaseTableViewCell { |
| 16 | + private enum Layout { |
| 17 | + static let buttonTrailingSpacing: CGFloat = 20 |
| 18 | + static let buttonHeight: CGFloat = 30 |
| 19 | + static let buttonPadding: CGFloat = 10 |
| 20 | + static let buttonCornerRadius: CGFloat = 4 |
| 21 | + } |
| 22 | + |
| 23 | + private let button = UIButton() |
| 24 | + private var buttonWidthConstraint: Constraint? |
| 25 | + weak var delegate: BitnagilButtonTableViewCellDelegate? |
| 26 | + |
| 27 | + override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { |
| 28 | + super.init(style: style, reuseIdentifier: reuseIdentifier) |
| 29 | + } |
| 30 | + |
| 31 | + required init?(coder: NSCoder) { |
| 32 | + fatalError("init(coder:) has not been implemented") |
| 33 | + } |
| 34 | + |
| 35 | + func configure( |
| 36 | + title: String, |
| 37 | + buttonTitle: String, |
| 38 | + isButtonEnabled: Bool |
| 39 | + ) { |
| 40 | + super.configure(title: title) |
| 41 | + |
| 42 | + let font = BitnagilFont.init(style: .body2, weight: .semiBold).font |
| 43 | + let textAttribute = [NSAttributedString.Key.font: font] |
| 44 | + let textWidth = (buttonTitle as NSString).size(withAttributes: textAttribute).width |
| 45 | + let buttonWidth = textWidth + Layout.buttonPadding * 2 |
| 46 | + |
| 47 | + buttonWidthConstraint?.update(offset: buttonWidth) |
| 48 | + button.setTitle(buttonTitle, for: .normal) |
| 49 | + button.isEnabled = isButtonEnabled |
| 50 | + |
| 51 | + if isButtonEnabled { |
| 52 | + button.backgroundColor = BitnagilColor.lightBlue100 |
| 53 | + button.setTitleColor(BitnagilColor.navy500, for: .normal) |
| 54 | + } else { |
| 55 | + button.backgroundColor = BitnagilColor.gray98 |
| 56 | + button.setTitleColor(BitnagilColor.gray70, for: .disabled) |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + override func configureAttribute() { |
| 61 | + super.configureAttribute() |
| 62 | + |
| 63 | + button.titleLabel?.font = BitnagilFont.init(style: .body2, weight: .semiBold).font |
| 64 | + button.layer.cornerRadius = Layout.buttonCornerRadius |
| 65 | + button.addAction( |
| 66 | + UIAction { [weak self] _ in |
| 67 | + guard let self else { return } |
| 68 | + self.delegate?.bitnagilButtonTableViewCellDidTapButton(self) |
| 69 | + }, |
| 70 | + for: .touchUpInside) |
| 71 | + } |
| 72 | + |
| 73 | + override func configureLayout() { |
| 74 | + super.configureLayout() |
| 75 | + |
| 76 | + contentView.addSubview(button) |
| 77 | + |
| 78 | + button.snp.makeConstraints { make in |
| 79 | + make.centerY.equalToSuperview() |
| 80 | + make.trailing.equalToSuperview().inset(Layout.buttonTrailingSpacing) |
| 81 | + make.height.equalTo(Layout.buttonHeight) |
| 82 | + buttonWidthConstraint = make.width |
| 83 | + .equalTo(Layout.buttonPadding) |
| 84 | + .constraint |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments