티스토리 뷰
다음은Label에 동일하게 적용되는 공통 스타일을 사용자 정의 LabelStyle을 만들고, LabelStyle에 사용자 정의 스타일을 확장하고 사용하는 예제이다.
import SwiftUI
struct TrailingIconLabelStyle: LabelStyle {
func makeBody(configuration: Configuration) -> some View {
HStack {
configuration.title
configuration.icon
}
}
}
extension LabelStyle where Self == TrailingIconLabelStyle {
static var trailingIcon: Self { Self() }
}
확장에서 보면 where Self == TrailingIconLabelStyle 구문이 특이하다. 이렇게 작성하면 LabelStyle이 trailingIcon을 사용할 때에만 해당 확장이 적용된다고 한다.
이렇게 만들어진 LabelStyle은 다음과 같이 사용하면 된다.
Label("\(scrum.lengthInMinutes)", systemImage: "clock")
.labelStyle(.trailingIcon)
'SwiftUI > Views' 카테고리의 다른 글
[SwiftUI] TextEditor 배경색 변경하기 (0) | 2023.08.26 |
---|---|
[SwiftUI] Form 배경색상, 시스템 색상 얻는 방법 (0) | 2023.08.24 |
[SwiftUI] 페이지 뷰 만들기 (0) | 2023.08.24 |
[SwiftUI] EditButton 사용 시, EditMode 감지하기 (0) | 2023.08.23 |
[SwiftUI] TextField가 나타날 때, 텍스트 전부 선택되게 하기 (0) | 2023.08.23 |
댓글