-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBKTextButton.swift
More file actions
172 lines (140 loc) · 4.47 KB
/
BKTextButton.swift
File metadata and controls
172 lines (140 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright © 2025 Booket. All rights reserved
import SnapKit
import UIKit
/// 밑줄이 있는 텍스트 버튼 컴포넌트입니다.
///
/// 링크 스타일의 텍스트 버튼으로, 배경 없이 텍스트와 밑줄만 표시됩니다.
/// `BKButtonGroup`과 함께 사용할 수 있습니다.
public final class BKTextButton: UIControl {
// MARK: - Public Properties
/// 버튼에 표시될 텍스트
public var title: String? {
didSet {
titleLabel.text = title
invalidateIntrinsicContentSize()
}
}
/// 버튼의 비활성화 여부
public var isDisabled: Bool = false {
didSet {
isEnabled = !isDisabled
updateColors()
}
}
/// 버튼 크기 (폰트 크기에 영향)
public var size: BKButtonSize = .small {
didSet {
titleLabel.font = size.font
invalidateIntrinsicContentSize()
}
}
/// 텍스트 및 밑줄 색상 설정
public var foregroundColors: BKButtonColorSet = BKButtonColorSet(
normal: .bkContentColor(.brand),
pressed: .bkContentColor(.brand),
disabled: .bkContentColor(.disable)
) {
didSet {
updateColors()
}
}
// MARK: - Override Properties
public override var isHighlighted: Bool {
didSet {
updateColors()
animatePressedState()
}
}
public override var isEnabled: Bool {
didSet {
updateColors()
}
}
// MARK: - Private Properties
private let titleLabel = UILabel()
private let underlineView = UIView()
private let animationDuration: TimeInterval = 0.15
private var currentState: BKButtonState {
BKButtonState(isEnabled: isEnabled, isHighlighted: isHighlighted)
}
// MARK: - Initialization
public init(title: String? = nil, size: BKButtonSize = .small) {
super.init(frame: .zero)
self.title = title
self.size = size
setupViews()
updateColors()
}
public required init?(coder: NSCoder) {
super.init(coder: coder)
setupViews()
updateColors()
}
// MARK: - Setup
private func setupViews() {
setupTitleLabel()
setupUnderlineView()
}
private func setupTitleLabel() {
addSubview(titleLabel)
titleLabel.text = title
titleLabel.font = size.font
titleLabel.textAlignment = .center
titleLabel.numberOfLines = 1
titleLabel.snp.makeConstraints {
$0.top.leading.trailing.equalToSuperview()
}
}
private func setupUnderlineView() {
addSubview(underlineView)
underlineView.snp.makeConstraints {
$0.top.equalTo(titleLabel.snp.bottom).offset(1)
$0.leading.trailing.equalTo(titleLabel)
$0.height.equalTo(1)
$0.bottom.equalToSuperview()
}
}
// MARK: - Update Methods
private func updateColors() {
let color = foregroundColors.color(for: currentState)
titleLabel.textColor = color
underlineView.backgroundColor = color
}
// MARK: - Animation
private func animatePressedState() {
let targetAlpha: CGFloat = isHighlighted ? 0.6 : 1.0
UIView.animate(
withDuration: animationDuration,
delay: 0,
options: [.allowUserInteraction, .beginFromCurrentState],
animations: {
self.titleLabel.alpha = targetAlpha
self.underlineView.alpha = targetAlpha
}
)
}
// MARK: - Intrinsic Content Size
public override var intrinsicContentSize: CGSize {
let labelSize = titleLabel.intrinsicContentSize
// 라벨 높이 + 밑줄 오프셋(1) + 밑줄 높이(1)
return CGSize(
width: labelSize.width,
height: labelSize.height + 2
)
}
}
// MARK: - Factory Methods
extension BKTextButton {
/// 텍스트 버튼 생성
public static func text(title: String, size: BKButtonSize = .small) -> BKTextButton {
BKTextButton(title: title, size: size)
}
}
/* 커스텀 색상세트 사용 시
let customButton = BKTextButton(title: "커스텀", size: .small)
customButton.foregroundColors = BKButtonColorSet(
normal: .systemBlue,
pressed: .systemBlue,
disabled: .systemGray
)
*/