Swift UITextField用法詳解

創(chuàng)建實(shí)例,設(shè)置尺寸

let textField=UITextField(frame: CGRect(x: 95, y: 480, width: 200, height: 40))
view.addSubview(textField)

設(shè)置邊框風(fēng)格

  • none 無邊框
  • line 直角矩形邊界線
  • bezel 有陰影的邊框
  • roundedRect 圓角矩形邊框
textField.borderStyle = .line

設(shè)置文字顏色

textField.textColor=UIColor.green

設(shè)置對齊方式

textField.textAlignment = .center

設(shè)置提示文字

textField.placeholder="請輸入姓名"

設(shè)置左右視圖

        // 左視圖
        let leftView = UILabel()
        leftView.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
        leftView.text="持卡人"
        leftView.textColor=UIColor.black
        leftView.font=UIFont.systemFont(ofSize: 18)
        // 右視圖
        let rightButton = UIButton(type: .infoDark)
        rightButton.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
        // 設(shè)置左右視圖
        textField.leftView=leftView
        textField.rightView=rightButton
        // 設(shè)置視圖模式
        textField.rightViewMode = .always
        textField.leftViewMode = .always

設(shè)置右側(cè)清除按鈕

textField.clearButtonMode = .always

clearButtonMode的作用是控制右側(cè)清除按鈕什么時(shí)候顯示,由枚舉UITextFieldViewMode控制:

  • never 從不顯示
  • whileEditing 開始編輯的時(shí)候
  • unlessEditing 除了編輯外都出現(xiàn)
  • always 一直出現(xiàn)

設(shè)置代理

  • 首先添加協(xié)議頭
class UIPage: UIViewController ,UITextFieldDelegate {
}
  • 然后設(shè)置代理
textField.delegate=self
  • 實(shí)現(xiàn)相關(guān)代理方法
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        print("將要開始編輯")
        return true
    }
    
    func textFieldDidBeginEditing(_ textField: UITextField) {
        print("已經(jīng)開始編輯")
    }
    
    func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
        print("將要結(jié)束編輯")
        return true
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        print("已經(jīng)結(jié)束編輯")
    }
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        print("文本輸入內(nèi)容將要發(fā)生變化(每次輸入都會(huì)調(diào)用)")
        return true
    }
    
    func textFieldShouldClear(_ textField: UITextField) -> Bool {
        print("將要清除輸入內(nèi)容,返回值是是否要清除掉內(nèi)容")
        return true
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        print("將要按下Return按鈕,返回值是是否結(jié)束輸入(是否失去焦點(diǎn))")
        return true
    }

設(shè)置完成按鈕樣式

textField.returnKeyType = UIReturnKeyType.done

設(shè)置鍵盤輸入樣式

  • default 默認(rèn)鍵盤:支持所有字符
  • asciiCapable 支持ASCII的默認(rèn)鍵盤
  • numbersAndPunctuation 標(biāo)準(zhǔn)電話鍵盤,支持+*#等符號(hào)
  • URL URL鍵盤,有.com按鈕;只支持URL字符
  • numberPad 數(shù)字鍵盤
  • phonePad 電話鍵盤
  • namePhonePad 電話鍵盤,也支持輸入人名字
  • emailAddress 用于輸入電子郵件地址的鍵盤
  • asciiCapableNumberPad 支持ASCII的數(shù)字鍵盤
  • decimalPad 帶‘.’的數(shù)字鍵盤
  • twitter 功能齊全鍵盤,類似asciiCapable
  • webSearch 帶有面向url的附加的默認(rèn)鍵盤類型
textField.keyboardType = UIKeyboardType.numberPad

設(shè)置鍵盤外觀主題

  • default 白色,這個(gè)字段是為了兼容以前的版本
  • dark 黑色
  • light 白色
  • alert 黑色,這個(gè)字段是為了兼容以前的版本
textField.keyboardAppearance = UIKeyboardAppearance.light

設(shè)置安全文本(輸入密碼時(shí)使用)

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

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

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