아래 코드는 UIButton.Configuration API(UIKit iOS 15+)로 “오늘” 버튼을 구성하는 예시다.
텍스트/아이콘/여백/배경/테두리/코너를 설정하고, 가로 압축 우선순위를 높여 잘림을 방지한다.
let todayButton = UIButton(type: .system).then { btn in
var config = UIButton.Configuration.plain()
config.baseForegroundColor = .primary600
// 텍스트
let title = Typography.attributed(
"오늘",
style: .labelMd(weight: .semibold),
color: .primary600
)
config.attributedTitle = AttributedString(title)
// 아이콘 (왼쪽)
config.image = UIImage.rotateCcw.withRenderingMode(.alwaysTemplate)
config.imagePlacement = .leading
config.imagePadding = Metrics.todayImageSpacing
// 바깥 여백
config.contentInsets = .init(
top: Metrics.todayVerticalPadding,
leading: Metrics.todayHorizontalPadding,
bottom: Metrics.todayVerticalPadding,
trailing: Metrics.todayHorizontalPadding
)
// 배경/테두리/코너
var bg = UIBackgroundConfiguration.clear()
bg.backgroundColor = .primary100
bg.strokeColor = .primary200
bg.strokeWidth = 1
bg.cornerRadius = 18
config.background = bg
btn.configuration = config
// 잘림 방지
btn.setContentCompressionResistancePriority(.required, for: .horizontal)
}
.plain()은 최소한의 배경/레이아웃을 제공하고, 텍스트/아이콘/여백을 개발자가 설정한다.btn.configuration = config로 최종 적용.withRenderingMode(.alwaysTemplate)일 때 색상이 적용된다.tintColor를 따로 만지지 않아도 구성값이 우선 반영된다.Typography.attributed(...)로 만든 NSAttributedString을 AttributedString으로 변환해 설정.setAttributedTitle 대신 config.attributedTitle을 사용.config.image에 심볼/커스텀 이미지를 지정.imagePlacement = .leading으로 아이콘을 텍스트 왼쪽에 배치.imagePadding으로 아이콘–텍스트 간격을 제어.UIBackgroundConfiguration.clear()로 시작해 필요한 속성만 지정.backgroundColor: 배경색strokeColor, strokeWidth: 테두리cornerRadius: 모서리 반경layer.*보다 background 설정이 우선이며 상태별 자동 처리에 유리.setContentCompressionResistancePriority(.required, .horizontal)로 가로 압축 저항을 최상으로 올려구성 기반 버튼은 상태별 속성 오버라이드가 가능하다.
config.baseForegroundColor = .primary600
config.background.backgroundColor = .primary100
btn.configurationUpdateHandler = { button in
guard var cfg = button.configuration else { return }
switch button.state {
case .highlighted:
cfg.background?.backgroundColor = .primary200
case .disabled:
cfg.baseForegroundColor = .primary300
cfg.background?.strokeColor = .primary200
default:
break
}
button.configuration = cfg
}
configurationUpdateHandler에서 button.state를 보고 색/테두리/알파 등을 조절.isEnabled, isHighlighted, isSelected 조합도 처리 가능.todayButton.titleLabel?.lineBreakMode = .byTruncatingTail
todayButton.titleLabel?.adjustsFontForContentSizeCategory = truetodayButton.setPreferredSymbolConfiguration(
.init(pointSize: 14, weight: .medium),
forImageIn: .normal
)config.title = "오늘"
config.subtitle = nilvar config = UIButton.Configuration.filled()
config.baseBackgroundColor = .primary500
config.baseForegroundColor = .whiteUIButton.Configuration 사용.baseForegroundColor가 반영됨.UIBackgroundConfiguration에서 지정.configurationUpdateHandler에서 일관 처리.baseForegroundColor와 UIBackgroundConfiguration을 조합하면 디자인 시스템을 깔끔하게 반영할 수 있다.