6.1、UITextField 、UITextView

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.

? ? }

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容