Style이 비슷한 여러 객체를 ViewModifier
를 통해 간략히 표현할 수 있다.
매개변수를 통해 style 일부분을 다르게 할 수도 있다.
…
Text(“txt1”).modifier(MyTextStyle())
Text(“txt2”).modifier(MyTextStyle())
Text(“txt3”).modifier(MyTextStyle(myWeight:.bold))
…
struct MyTextStyle: ViewModifier {
var myWeight = Font.Weight.reqular
var myFont = Font.title2
func body(content: Content) -> some View {
content
.font(myFont.weight(myWeight))
.foregroundColor(.orange)
.padding(.bottom, 20)
}
}
객체에 custom .prototype
을 만들어서 간편히 사용할 수 있다.
…
Text(“txt”).customFont()
…
extension Text {
func customFont() -> Text {
self
.font(.title2)
.bold()
.italic()
.foregroundColor(.blue)
}
}
기본적인 확인 alert
Button(“Show alert”) {
isShowAlert.toggle()
}
.alert(isPresented: $isShowAlert, content: {
Alert(title: Text(“hello alert text!!”))
})
Alert(title: Text(“hello alert text!!”), dismissButton: .cancel())
각primary
버튼, secondary
버튼 눌렀을때 변수 변환 예시코드
@state private var selectText = “none”;
…
.alert(isPresented: $isShowAlert, content: {
let primaryBtn =
Alert.Button.default(Text(“done”)) {
selectText = “done”
}
let secondaryBtn =
Alert.Button.default(Text(“cancel”)) {
selectText = “cancel”
}
return Alert(
title: Text(“hello alert text!!”),
primaryButton: primaryBtn,
secondaryButton: secondaryBtn
)
})