Textsms UIkit의 Label과 같은역할 다른 뷰객체에서 UI구성시 텍스트를 사용할떄 사용됨.
예제)
struct ContentView: View {
var body: some View {
VStack(spacing:30) {
Text("폰트와 굵기 설정")
.font(.title)
.fontWeight(.black)
Text("글자색은 ForeGround 배경은 Background")
.foregroundColor(.white)
.padding()
.background(Color.blue)
Text("커스텀 폰트, 볼드체, 이탤릭체, 밑줄, 취소선")
.font(.custom("Menlo", size: 16))
.bold()
.italic()
.underline()
.strikethrough()
Text("라인 수 제한과 \\n 텍스트 정렬 기능입니다. \\n이건안보입니다.")
.lineLimit(2)
.multilineTextAlignment(.trailing)
.fixedSize()
(Text("자간과 기준선").kerning(11)+Text(" 조정도 쉽게 가능합니다.")).baselineOffset(2)
.font(.system(size:16))
//수식어의 순서가 중요함
Text("☠️👽😺😼😾")
.font(.largeTitle)
.background(Color.yellow)
.padding()
Text("☠️👽😺😼😾")
.font(.largeTitle)
.padding()
.background(Color.yellow)
}
}
}
Text에 정의된 font
public func font(_ font: Font?) -> Text
View에 정의된 font
@inlinable public func font(_ font: Font?) -> some View
→ 반환 타입이 서로 다름. 또 텍스트뷰에는 있지만 뷰프로토콜에는 있는 메소드도 있오 뷰 프로토콜에는 존재하지만 텍스트프로토콜에는 존재하지 않는 프로토콜이 있기 때문에 순서를 잘못 코딩하는 경우 컴파일 에러 또는 원하지 않는 화면으로 커스텀됨.
( 컴파일 오류)
Text("Swift UI")
.bold() ->Text
.padding() -> View
.font(.title) -> View
Text("Swift UI")
.padding() -> View
.bold() -> view 프로토콜에는 bold가없기 때문에 컴파일 에러 발생
.font(.title)
(순서 로 뷰가 달라짐)
//1
Text("☠️👽😺😼😾")
.font(.largeTitle)
.background(Color.yellow)
.padding()
//2
Text("☠️👽😺😼😾")
.font(.largeTitle)
.padding()
.background(Color.yellow)