Swift4 2 UITextField基本用法

先看效果图

github源码 github.com/daomoer/YYS… 初始化一个UITextField

let phoneTF = UITextField.init(frame: CGRect(x:15, y:400, width:UIScreen.main.bounds.width-30, height:30))
self.view.addSubview(phoneTF)
phoneTF.delegate = self
// 属性设置
phoneTF.placeholder = "水印文字"
//字体颜色
phoneTF.textColor = UIColor.purple
//字体大小
phoneTF.font = UIFont.systemFont(ofSize: 16)
//文字是否可以缩小,缩小到指定字号不再缩小
phoneTF.adjustsFontSizeToFitWidth = true
// 文字最小字号
phoneTF.minimumFontSize = 12
//设置是否加密模式
phoneTF.isSecureTextEntry = true
//记录之前输入的内容
phoneTF.autocorrectionType = UITextAutocorrectionType.no
复制代码

设置边框样式

//设置边框样式
phoneTF.borderStyle = UITextBorderStyle.roundedRect
/*
phoneTF.borderStyle = UITextBorderStyle.none
phoneTF.borderStyle = UITextBorderStyle.line
phoneTF.borderStyle = UITextBorderStyle.bezel
*/    
复制代码

文字内容对齐方式

phoneTF.textAlignment = NSTextAlignment.center
/*
phoneTF.textAlignment = NSTextAlignment.left
phoneTF.textAlignment = NSTextAlignment.right
phoneTF.textAlignment = NSTextAlignment.justified
phoneTF.textAlignment = NSTextAlignment.natural
*/
复制代码

内容水平对齐方式

phoneTF.contentHorizontalAlignment = UIControlContentHorizontalAlignment.center
/*
phoneTF.contentHorizontalAlignment = UIControlContentHorizontalAlignment.left
phoneTF.contentHorizontalAlignment = UIControlContentHorizontalAlignment.right
phoneTF.contentHorizontalAlignment = UIControlContentHorizontalAlignment.fill
*/
复制代码

内容垂直对齐方式

phoneTF.contentVerticalAlignment = UIControlContentVerticalAlignment.center
/*
phoneTF.contentVerticalAlignment = UIControlContentVerticalAlignment.top
phoneTF.contentVerticalAlignment = UIControlContentVerticalAlignment.bottom
phoneTF.contentVerticalAlignment = UIControlContentVerticalAlignment.fill
*/
复制代码

设置返回按钮样式

phoneTF.returnKeyType = UIReturnKeyType.done
/*
phoneTF.returnKeyType = UIReturnKeyType.default
phoneTF.returnKeyType = UIReturnKeyType.go
phoneTF.returnKeyType = UIReturnKeyType.google
phoneTF.returnKeyType = UIReturnKeyType.join
phoneTF.returnKeyType = UIReturnKeyType.next
phoneTF.returnKeyType = UIReturnKeyType.route
phoneTF.returnKeyType = UIReturnKeyType.search
phoneTF.returnKeyType = UIReturnKeyType.send
phoneTF.returnKeyType = UIReturnKeyType.yahoo
*/
复制代码

设置键盘样式

phoneTF.keyboardType = UIKeyboardType.numbersAndPunctuation
/*
phoneTF.keyboardType = UIKeyboardType.default
phoneTF.keyboardType = UIKeyboardType.asciiCapable
phoneTF.keyboardType = UIKeyboardType.emailAddress
phoneTF.keyboardType = UIKeyboardType.URL
phoneTF.keyboardType = UIKeyboardType.numberPad
phoneTF.keyboardType = UIKeyboardType.phonePad
phoneTF.keyboardType = UIKeyboardType.namePhonePad
*/
复制代码

设置键盘风格

phoneTF.keyboardAppearance = UIKeyboardAppearance.light
/*
 phoneTF.keyboardAppearance = UIKeyboardAppearance.default
 phoneTF.keyboardAppearance = UIKeyboardAppearance.dark
 phoneTF.keyboardAppearance = UIKeyboardAppearance.alert
 */
复制代码

设置清除按钮

phoneTF.clearButtonMode = UITextFieldViewMode.always
/*
 phoneTF.clearButtonMode = UITextFieldViewMode.never
 phoneTF.clearButtonMode = UITextFieldViewMode.whileEditing
 phoneTF.clearButtonMode = UITextFieldViewMode.unlessEditing
 */
复制代码

设置首字母是否大写

phoneTF.autocapitalizationType = UITextAutocapitalizationType.none
/*
 phoneTF.autocapitalizationType = UITextAutocapitalizationType.words
 phoneTF.autocapitalizationType = UITextAutocapitalizationType.sentences
 phoneTF.autocapitalizationType = UITextAutocapitalizationType.allCharacters
*/
复制代码

设置输入框左侧头像

let hadeimage = UIImageView.init(frame:CGRect(x:0, y:0, width:25, height:30))
hadeimage.image = UIImage.init(named: "testImage.png")
phoneTF.leftView = hadeimage
复制代码

设置左侧视图模式

phoneTF.leftViewMode = UITextFieldViewMode.always;
/*
 textField.leftViewMode = UITextFieldViewMode.whileEditing;
 textField.leftViewMode = UITextFieldViewMode.unlessEditing;
 textField.leftViewMode = UITextFieldViewMode.none;
 */
复制代码

UITextField代理方法使用

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
    //返回一个BOOL值,指定是否循序文本字段开始编辑
    return true;
}
复制代码
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
//返回BOOL值,指定是否允许文本字段结束编辑,当编辑结束,文本字段会让出first responder
//要想在用户结束编辑时阻止文本字段消失,可以返回NO
//这对一些文本字段必须始终保持活跃状态的程序很有用,比如即时消息
return false;
}
复制代码
func textFieldDidBeginEditing(_ textField: UITextField) {
    //开始编辑时触发,文本字段将成为first responder
}
复制代码
func textFieldShouldClear(_ textField: UITextField) -> Bool {
//返回一个BOOL值指明是否允许根据用户请求清除内容
//可以设置在特定条件下才允许清除内容
return true;
}
复制代码
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    //返回一个BOOL值,指明是否允许在按下回车键时结束编辑
    //如果允许要调用resignFirstResponder 方法,这回导致结束编辑,而键盘会被收起[textField resignFirstResponder];
    //查一下resign这个单词的意思就明白这个方法了
    return true;
}
复制代码
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    //当用户使用自动更正功能,把输入的文字修改为推荐的文字时,就会调用这个方法。
    //这对于想要加入撤销选项的应用程序特别有用
    //可以跟踪字段内所做的最后一次修改,也可以对所有编辑做日志记录,用作审计用途。
    //要防止文字被改变可以返回NO
    //这个方法的参数中有一个NSRange对象,指明了被改变文字的位置,建议修改的文本也在其中
    return true;
}
复制代码

转载于:https://juejin.im/post/5c88e4eaf265da2dd37c5d18

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值