//
// ViewController.swift
// Label
//
// Created by 赵士军 on 2019/11/18.
// Copyright © 2019 赵士军. All rights reserved.
//
import UIKit
class ViewController: UIViewController ,UITextFieldDelegate{
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.getTextField()
}
@objc func getTextField(){
self.view.backgroundColor = .red
let textField = UITextField.init(frame: CGRect(x: 0, y: 0, width: 200, height: 40))
textField.placeholder = "请输入"
textField .becomeFirstResponder()
textField.textAlignment = .left
textField.delegate=self
textField.font = .systemFont(ofSize: 15)
textField.center=self.view.center
textField.keyboardType = .default
textField.borderStyle = .roundedRect
textField.clearButtonMode = .whileEditing
textField.isSecureTextEntry = false
textField.leftViewMode = .always
textField.rightViewMode = .always
let leftButton = UIButton(type: .infoDark)
leftButton.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
textField.leftView = leftButton
let rightButton = UIButton(type: .contactAdd)
rightButton.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
textField.rightView = rightButton
textField.keyboardAppearance = UIKeyboardAppearance.alert
self.view .addSubview(textField)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view .endEditing(true)
}
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
print("将要开始编辑")
return true
}
func textFieldDidBeginEditing(_ textField: UITextField) {
print("已经开始编辑")
}
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
print("将要结束编辑")
return true
}
func textFieldDidEndEditing(_ textField: UITextField) {
print("已经结束编辑")
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
print("文本输入内容将要发生变化(每次输入都会调用)")
return true
}
func textFieldShouldClear(_ textField: UITextField) -> Bool {
print("将要清除输入内容,返回值是是否要清除掉内容")
return true
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
print("将要按下Return按钮,返回值是是否结束输入(是否失去焦点)")
return true
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}