import UIKit
class ViewController: UIViewController,UITextFieldDelegate,UITextViewDelegate {
overridefuncviewDidLoad() {
super.viewDidLoad()
self.createUITextField()
self.createUITextview()
self.adjustTextView()
}
funccreateUITextField() {
lettextField =UITextField(frame:CGRect(x:20, y:270, width:250, height:40))
textField.backgroundColor = UIColor.cyan
textField.borderStyle= .line
textField.placeholder=" 请输入用户名"
textField.textColor=UIColor.brown
textField.font=UIFont.systemFont(ofSize:15)
textField.textAlignment= .natural
textField.adjustsFontSizeToFitWidth = true
textField.clearButtonMode= .whileEditing
// textField.delegate = self as? UITextFieldDelegate
textField.delegate=self
self.view.addSubview(textField)
letimgv =UIImageView(frame:CGRect(x:20, y:8, width:40, height:24))
imgv.image=UIImage(named:"dl-iphone")
imgv.contentMode= .center
textField.leftView= imgv
textField.leftViewMode= .always
textField.leftView?.backgroundColor = UIColor.red
letpasswordTf =UITextField(frame:CGRect(x:20, y:380, width:250, height:40))
self.view.addSubview(passwordTf)
passwordTf.isSecureTextEntry=true
passwordTf.becomeFirstResponder()
passwordTf.keyboardType= .decimalPad
passwordTf.returnKeyType= .next
passwordTf.borderStyle= .roundedRect
passwordTf.backgroundColor = UIColor.cyan
passwordTf.addTarget(self, action:#selector(changeValue(tf:)), for: .editingChanged)
}
funcchangeValue(tf:UITextField) {
print(tf.text??"")
}
functextFieldShouldBeginEditing(_textField:UITextField) ->Bool{
print("我将要开始编辑了")
return true
}
functextFieldDidBeginEditing(_textField:UITextField) {
print("我正在编辑中")
}
functextFieldShouldEndEditing(_textField:UITextField) ->Bool{
print("我即将结束编辑状态")
textField.resignFirstResponder()
return true
}
functextFieldDidEndEditing(_textField:UITextField) {
print("我已结束编辑状态")
}
functextFieldShouldReturn(_textField:UITextField) ->Bool{
print("输入框按下键盘return收回键盘")
return true
}
functextField(_textField:UITextField, shouldChangeCharactersIn range:NSRange, replacementString string:String) ->Bool{
print("文本框内容变化时调用")
return true
}
funccreateUITextview(){
lettextview =UITextView(frame:CGRect(x:20, y:430, width:250, height:100))
self.view.addSubview(textview)
// textview.delegate = self
textview.backgroundColor = UIColor.lightGray
textview.textColor=UIColor.blue
textview.tintColor=UIColor.red//光标颜色
textview.keyboardType= .webSearch
textview.font=UIFont.systemFont(ofSize:20)
textview.showsHorizontalScrollIndicator = true
}
funcadjustTextView(){
lettextF =UITextView()
self.view.addSubview(textF)
textF.backgroundColor = UIColor.brown
textF.text = "回合肥 iu 额外花费就是看到 但是菲尼克斯南方电视 额罚款金额为南方就是你看到就能放开手的都送 if 就哦IE家唯饭呢咖啡呢今年的就是看到合肥无法 i 额外粉红色可能你今年的 v 计划 iu how IE人前闻鸡起舞 i 觉得呢"
letstr:NSString= textF.text!asNSString
textF.font=UIFont.systemFont(ofSize:16)
letrect = str.boundingRect(with:CGSize(width:300,height:0), options:NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSFontAttributeName:textF.font!], context:nil)
// textF.adjustsFontSizeToFitWidth = true
textF.delegate=self
textF.backgroundColor = UIColor.blue
textF.frame=CGRect(x:20, y:540, width:300, height: rect.height)
}
functextView(_textView:UITextView, shouldChangeTextIn range:NSRange, replacementText text:String) ->Bool{
letstr:NSString= textView.text!asNSString
letrect = str.boundingRect(with:CGSize(width:300,height:0), options:NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSFontAttributeName:textView.font!], context:nil)
textView.frame=CGRect(x:20, y:540, width:300, height: rect.height)
return true
}
functextViewShouldEndEditing(_textView:UITextView) ->Bool{
textView.endEditing(true)
print("textView将要结束编辑状态")
return true
}
overridefuncdidReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}