UITextField的使用
mport UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.setupTextField()
}
func setupTextField() {
// 创建textField
let textField = UITextField(frame: CGRect(x: 100, y: 100, width: 100, height: 40))
// 设置输入框的边框样式
/**
public enum UITextBorderStyle : Int {
case none 无边框
case line 直线边框
case bezel 边线+阴影
case roundedRect 圆角矩形边框
}
*/
textField.borderStyle = .none
// 设置提示文字
textField.placeholder = "请输入相关文字"
// 设置使用*** 显示, 如密码
textField.isSecureTextEntry = true
// 当文字超出文本框宽度时,自动调整文字大小
textField.adjustsFontSizeToFitWidth = true
// 最小可缩小的字号
textField.minimumFontSize = 12.0
// 水平对齐
/**
public enum NSTextAlignment : Int {
case left // Visually left aligned
case center // Visually centered
case right // Visually right aligned
case justified // Fully-justified. The last line in a paragraph is natural-aligned.
case natural // Indicates the default alignment for script
}
*/
textField.textAlignment = .center
// 垂直对齐
/**
public enum UIControlContentVerticalAlignment : Int {
case center 垂直居中对齐
case top 垂直向上对齐
case bottom 垂直向下对齐
case fill
}
*/
textField.contentVerticalAlignment = .top
// 设置背景图片,先要去除边框样式
textField.borderStyle = .none
textField.background = UIImage(named: "back")
// 清除按钮
/**
public enum UITextFieldViewMode : Int {
case never 从不出现
case whileEditing 编辑时出现清除按钮
case unlessEditing 编辑时不出现,编辑后才出现清除按钮
case always 一直显示清除按钮
}
*/
textField.clearButtonMode = .whileEditing
// 键盘类型
/**
public enum UIKeyboardType : Int {
case `default` // Default type for the current input method. 系统默认的虚拟键盘
case asciiCapable // Displays a keyboard which can enter ASCII characters 显示英文字母的虚拟键盘
case numbersAndPunctuation // Numbers and assorted punctuation. 显示数字和标点的虚拟键盘
case URL // A type optimized for URL entry (shows . / .com prominently). 显示便于输入数字的虚拟键盘
case numberPad // A number pad with locale-appropriate digits (0-9, ۰-۹, ०-९, etc.). Suitable for PIN entry. 显示便于输入数字的虚拟键盘
case phonePad // A phone pad (1-9, *, 0, #, with letters under the numbers). 显示便于拨号呼叫的虚拟键盘
case namePhonePad // A type optimized for entering a person's name or phone number. 显示便于聊天拨号的虚拟键盘
case emailAddress // A type optimized for multiple email address entry (shows space @ . prominently). 显示便于输入Email的虚拟键盘
@available(iOS 4.1, *)
case decimalPad // A number pad with a decimal point. 显示用于输入数字和小数点的虚拟键盘
@available(iOS 5.0, *)
case twitter // A type optimized for twitter text entry (easy access to @ #) 显示方便些Twitter的虚拟键盘
@available(iOS 7.0, *)
case webSearch // A default keyboard type with URL-oriented addition (shows space . prominently). 显示便于在网页上书写的虚拟键盘
@available(iOS 10.0, *)
case asciiCapableNumberPad // A number pad (0-9) that will always be ASCII digits.
public static var alphabet: UIKeyboardType { get } // Deprecated
}
*/
textField.keyboardType = .default
// 成为第一响应者
textField.becomeFirstResponder()
// 放弃第一响应者
textField.resignFirstResponder()
// 设置键盘return键的样式
/**
public enum UIReturnKeyType : Int {
case `default`
case go 表示完成输入,同时会跳到另一页
case google
case join 表示注册用户或添加数据
case next 表示继续下一步
case route
case search 表示搜索
case send 表示继续下一步
case yahoo
case done 表示完成输入
case emergencyCall
@available(iOS 9.0, *)
case `continue`
}
*/
textField.returnKeyType = .done
// 设置代理
textField.delegate = self
// 设置attributedPlaceholder
// 1. 创建属性字典,存放需要修改的属性
var attributed:[String: AnyObject] = NSMutableDictionary() as! [String: AnyObject]
attributed[NSFontAttributeName] = UIFont.systemFont(ofSize: 13)
// 2. 创建带属性的字符串
let string:NSAttributedString = NSAttributedString.init(string: "你还好嘛,hello world", attributes: attributed)
// 3. 设置placeholder属性
textField.attributedPlaceholder = string
/**
public enum UITextFieldViewMode : Int {
case never
case whileEditing
case unlessEditing
case always
}
*/
textField.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
textField.leftViewMode = .always
self.view.addSubview(textField)
}
}
extension ViewController: UITextFieldDelegate {
// 点击return键时调用
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
return true
}
// 将要开始输入的时候调用
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return true
}
// 已经开始输入
func textFieldDidBeginEditing(_ textField: UITextField) {
}
// 将要结束输入时调用
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
return true
}
// 已经输入结束
func textFieldDidEndEditing(_ textField: UITextField) {
}
// 清除文字按钮的点击事件
func textFieldShouldClear(_ textField: UITextField) -> Bool {
return true
}
}
网友评论