[SwiftUI fundamental Tutorial] TextField, SecureField

2021. 9. 14. 17:33·강의/etc

위 글은 유튜브 정대리님의 SwiftUI fundamental Tutorial 강좌를 보고 작성한 정리글로

자세한 내용은 유튜브를 통해 확인하시길 권장합니다. 

 

 

 

 

 

 

일반적인 TextField 

import SwiftUI

struct ContentView: View {
    
    //text가 받는 변수 선언
    @State private var inputValue : String = ""
    
    var body: some View {
        //text가 Binding으로 감싸져있는 형태라 $ 필수
        TextField("글자를 입력하세요", text: $inputValue)
        
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

TextField 기본 형태

TextField("글자를 입력하기 전에 보여주는 곳", text: Binding<String>)

 

text가 받는 변수를 선언해줌 (ContentView에서만 사용할거라 private 형태로 사용함)

@State private var inputValue : String = ""

 

text:가 Binding 형태라 TextField에 변수에 $ 필수

 

 

 

 

간단한 아이디 / 비밀번호 폼 만들기

import SwiftUI

struct ContentView: View {
    
    //text가 받는 변수 선언
    @State private var id : String = ""
    @State private var password : String = ""
    
    var body: some View {
        VStack(spacing: 10){
            
            HStack{
                
                TextField("아이디 입력", text: $id)
                    .autocapitalization(.none)
                    .disableAutocorrection(true)
                    //입력 글자 설정
                    //.allCharacters: 모든 글자를 대문자로
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                
                Button(action:{
                    self.id = ""
                }){
                    if(self.id.count > 0){
                    Image(systemName: "multiply.circle.fill")
                        .font(.system(size: 20))
                        .foregroundColor(.secondary)
                    }
                }
                
            }
            
            HStack{
                
                SecureField("비밀번호 입력", text: $password)
                    .autocapitalization(.none)
                    .disableAutocorrection(true)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                
                Button(action:{
                    self.password = ""
                }){
                    if(self.password.count > 0){
                    Image(systemName: "multiply.circle.fill")
                        .font(.system(size: 20))
                        .foregroundColor(.secondary)
                    }
                }
            }
            
            
            
            Text("비번 확인하기: \(password)")
            
        }.padding(.horizontal, 50)
        
        
        
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

.textFieldStyle(): 텍스트 필드 스타일 설정

 

.autocapitalization(): 자동으로 글자를 대문자로 전환 시켜줌

.disableAutocorrection(): 자동으로 글자가 틀렸는지 안틀렸는지 옵션을 꺼주는 것 

 

if (self.id.count > 0)과 if (self.password.count > 0)을 사용해서 글자가 들어오면 곱표 아이콘이 생기도록 구현

 

최종 화면

'강의 > etc' 카테고리의 다른 글

[SwiftUI fundamental Tutorial] Picker, SegmentedStyle  (0) 2021.10.05
[SwiftUI fundamental Tutorial] Toast, Popup  (0) 2021.09.16
[SwiftUI fundamental Tutorial] ButtonStyle  (0) 2021.09.14
[SwiftUI fundamental Tutorial] Custom TabView  (0) 2021.09.01
[SwiftUI fundamental Tutorial] TabView  (0) 2021.08.31
'강의/etc' 카테고리의 다른 글
  • [SwiftUI fundamental Tutorial] Picker, SegmentedStyle
  • [SwiftUI fundamental Tutorial] Toast, Popup
  • [SwiftUI fundamental Tutorial] ButtonStyle
  • [SwiftUI fundamental Tutorial] Custom TabView
덩이
덩이
찍먹 대마왕
  • 덩이
    Devlog
    덩이
  • 전체
    오늘
    어제
    • 분류 전체보기 (118)
      • 강의 (68)
        • SAP ERP (11)
        • KOSTA (32)
        • Inflearn (0)
        • etc (25)
      • 회사 (0)
        • 스터디 (3)
        • 전자정부 (0)
      • 학교 (15)
      • 스터디 (30)
        • 알고리즘 (25)
        • 프로젝트 (3)
        • 에러 (2)
        • 자격증 (0)
      • 기타 (2)
        • 자료 (1)
        • 회고록 (1)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

    • GitHub
    • Naver
  • 공지사항

  • 인기 글

  • 태그

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
덩이
[SwiftUI fundamental Tutorial] TextField, SecureField
상단으로

티스토리툴바