티스토리 뷰

다음은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)
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/07   »
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
글 보관함