개발/SWIFT 아이폰 / / 2023. 7. 17. 14:58

SwiftUI TextField 입력시 버튼 색상, isEnabled 변경

반응형

텍스트 필드 10글자 입력시 버튼 색상 및 isEnabled 설정 변경

struct ContentView: View {
    @State private var inputText: String = ""
    @State private var isInputFocused: Bool = false
    
    var body: some View {
        VStack {
            TextField("텍스트를 입력하세요", text: $inputText, onEditingChanged: { editing in
                isInputFocused = editing
            })
            .textFieldStyle(PlainTextFieldStyle())
            .foregroundColor(.blue)        // 글자 컬러
            .accentColor(.blue)
            .padding(10)    // 외부 패딩 적용
            .font(.system(size: 20, weight: .heavy, design: .default))
            .background(RoundedRectangle(cornerRadius: 15).fill(Color.gray))
            .overlay(
                RoundedRectangle(cornerRadius: 15)
                    .stroke(lineWidth: 2)
                    .foregroundColor(isInputFocused ? .blue : .gray) // 입력 모드에 따라 색상 변경
            )
            .padding(10) // 내부 패딩 적용
            
            Button(action: {
                print("입력된 텍스트: \(inputText)")
            }) {
                Text("출력하기")
                    .padding()
                    .foregroundColor(.white)
                    .background(inputText.count >= 10 ? Color.red : Color.blue) // 조건에 따라 색상 변경
                    .cornerRadius(10)
            }
            .padding()
            .disabled(inputText.count < 10) // 조건에 따라 클릭 가능 여부 설정
        }.background(Color.white)
    }
}
반응형
  • 네이버 블로그 공유
  • 네이버 밴드 공유
  • 페이스북 공유
  • 카카오스토리 공유