SwiftUI實(shí)戰(zhàn)-自定義TextField搭建登錄頁(yè)面UI

SwiftUI實(shí)戰(zhàn)系列

本章內(nèi)容

實(shí)現(xiàn)登錄頁(yè)面UI基本搭建

效果圖:


自定義TextField.gif

將學(xué)到的內(nèi)容:
1、實(shí)現(xiàn)密碼輸入的SecureField模式
2、監(jiān)聽(tīng)輸入框的內(nèi)容變化
3、輸入限制字?jǐn)?shù)輸入
4、ViewModifier的拓展使用
5、PreferenceKey 傳值使用

相關(guān)源碼如下:
LoginView.swift

import SwiftUI

struct LoginView: View {
    
    //@EnvironmentObject var lauchViewVM: LauchViewModel
    @Environment(\.presentationMode) var presentationMode
    
    @State var account: String = ""
    @State var password: String = ""
    @State var content: String?
    @State var attributedText: NSAttributedString?
    @State var isSecureField = true
    @State var isShowClear = false
    
    var body: some View {
        /*
        // 輸入監(jiān)聽(tīng)
        let binding = Binding<String>(get: {
               self.account
           }, set: {
               debugPrint($0)
               self.account = $0
           })
         */
        
        
        VStack {
            VStack {
                HStack {
                    Text("賬號(hào):").padding(.horizontal, 10)
                    Spacer().frame(width: 0)
                    CSInputTextField.init(placeholder: "請(qǐng)輸入賬號(hào)", text: $account) { value in
                        //debugPrint(value)
                    }
                    .frame(height: 40)
                    .overlay(
                        RoundedRectangle(cornerRadius: 5, style: .continuous)
                            .stroke(.orange, lineWidth: 1.0)
                    )
                }
                
                /*
                HStack {
                    CSSTextView.init(placeholder: "請(qǐng)輸入內(nèi)容", attributedText: $attributedText, onCommit: { value in
                        debugPrint(value)
                    })
                    .frame(height: 100)
                    .overlay(
                        RoundedRectangle(cornerRadius: 5, style: .continuous)
                            .stroke(.orange, lineWidth: 1.0)
                    )
                }
                 */
                
                HStack {
                    Text("密碼:").padding(.horizontal, 10)
                    Spacer().frame(width: 0)
                    ZStack {
                        CSInputTextField.init(placeholder: "請(qǐng)輸入密碼", text: $password, isSecureField: isSecureField) { value in
                            debugPrint(value)
                        }
                        .frame(height: 40)
                        .preference(key: CustomPreferenceKey<String>.self, value: password)
                        .onPreferenceChange (CustomPreferenceKey<String>.self) { value in
                            //  只能傳到上一級(jí)的父組件
                            debugPrint(value)
                            if value.count > 6 {
                                password = String(value.prefix(6))
                            }
                            isShowClear = value.count > 0
                        }
                        HStack {
                            Spacer()
                            if isShowClear {
                                Image.init(systemName: "xmark")
                                    .resizable()
                                    .aspectRatio(contentMode: .fit)
                                    .padding(.trailing, 10)
                                    .frame(width: 20, height: 20)
                                    .onTapGesture {
                                        password = ""
                                    }
                            }

                            /*
                            Text(isSecureField ? "查看" : "隱藏")
                                .padding(.leading, 5)
                                .padding(.trailing, 5)
                                .onTapGesture {
                                isSecureField.toggle()
                            }
                             */
                        }
                    }
                    .overlay(
                        RoundedRectangle(cornerRadius: 5, style: .continuous)
                            .stroke(.orange, lineWidth: 1.0)
                    )
                }
                
                HStack {
                    Button.init {
                        
                    } label: {
                        Text("登錄")
                            .foregroundColor(Color.white)
                            .frame(maxWidth: .infinity, minHeight: 45, maxHeight: 45)
                            .background(Color.orange)
                            .cornerRadius(5)
                            .overlay(
                                RoundedRectangle(cornerRadius: 5, style: .continuous)
                                    .stroke(.orange, lineWidth: 1.0)
                            )
                        
                    }
                    
                }
                .frame(maxWidth: .infinity, minHeight: 45, maxHeight: 45)
            }
            .padding(.top, 50)
            .padding(.horizontal, 20)
            Spacer()
        }
        
        .navigationBarTitle("登錄", displayMode: .inline)
        .navigationBarItems(trailing: Button.init(action: {
            
        }, label: {
            NavigationLink.init(destination: RegisterView()) {
                Text("注冊(cè)")
            }
        }))
    }
}

CSInputTextField.swift

還有 38% 的精彩內(nèi)容
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
支付 ¥9.90 繼續(xù)閱讀

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容