티스토리 뷰

예제 코드

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
import SwiftUI
 
struct ContentView: View {
    
    var body: some View {
        VStack{
            Text("Main Title!")
                .font(.largeTitle)
                .foregroundColor(.black)
                .bold()
                .padding(.bottom, 20)
            // 일반 스타일
            
            Text("Sub Title!")
                .modifier(textStyle())
            // ViewModifier 사용
            
            Text("Description")
                .modifier(textStyle(myColor: .red))
            // ViewModifier 사용
 
            Text("Description")
                .CutomFont(color: .blue)
            // Text 에게 extension 적용하여 만든 함수
        }
    }
}
 
//ViewModifier
struct textStyle: ViewModifier {
    
    var myWeight = Font.Weight.regular
    var myFont = Font.title2
    var myColor = Color.black
    
    func body(content: Content) -> some View {
        content
            .font(myFont.weight(myWeight))
        .foregroundColor(.orange)
        .padding(.bottom, 20)
    }
    
}
 
//extension
extension Text {
    func CutomFont(color : Color) -> Text {
        self
            .font(.title2)
            .bold()
            .italic()
            .foregroundColor(color)
    }
}
 
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        Group {
            ContentView()
        }
    }
}
 
cs

 

결과

 

학습 개념

ViewModifer 사용

extension 사용

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