Swift ui 기초2

김부민·2021년 9월 2일
0

swift-ui

목록 보기
3/4

ViewModifier

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) 
      }
}

Swift 객체 .prototype 생성법

객체에 custom .prototype 을 만들어서 간편히 사용할 수 있다.

…
Text(“txt”).customFont()
…

extension Text {
   func customFont() -> Text {
       self
.font(.title2)
.bold()
.italic()
.foregroundColor(.blue)
   }
}

alert

기본적인 확인 alert

ok

Button(“Show alert”) {
  isShowAlert.toggle()
}
.alert(isPresented: $isShowAlert, content: {
    Alert(title: Text(“hello alert text!!”))
})

cancel

Alert(title: Text(“hello alert text!!”), dismissButton: .cancel())

primary, secondary alert

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
        )
})
profile
froent-developer

0개의 댓글