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=" 請(qǐng)輸入用戶名"

? ? ? ? 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("我將要開(kāi)始編輯了")

? ? ? ? return true

? ? }

? ? functextFieldDidBeginEditing(_textField:UITextField) {

? ? ? ? print("我正在編輯中")

? ? }

? ? functextFieldShouldEndEditing(_textField:UITextField) ->Bool{

? ? ? ? print("我即將結(jié)束編輯狀態(tài)")

? ? ? ? textField.resignFirstResponder()

? ? ? ? return true

? ? }

? ? functextFieldDidEndEditing(_textField:UITextField) {

? ? ? ? print("我已結(jié)束編輯狀態(tài)")

? ? }

? ? functextFieldShouldReturn(_textField:UITextField) ->Bool{

? ? ? ? print("輸入框按下鍵盤(pán)return收回鍵盤(pán)")

? ? ? ? return true

? ? }

? ? functextField(_textField:UITextField, shouldChangeCharactersIn range:NSRange, replacementString string:String) ->Bool{

? ? ? ? print("文本框內(nèi)容變化時(shí)調(diào)用")

? ? ? ? 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//光標(biāo)顏色

? ? ? ? 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 額外花費(fèi)就是看到 但是菲尼克斯南方電視? 額罰款金額為南方就是你看到就能放開(kāi)手的都送 if 就哦IE家唯飯呢咖啡呢今年的就是看到合肥無(wú)法 i 額外粉紅色可能你今年的 v 計(jì)劃 iu how IE人前聞雞起舞 i 覺(jué)得呢"

? ? ? ? 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將要結(jié)束編輯狀態(tài)")

? ? ? ? return true

? ? }



? ? overridefuncdidReceiveMemoryWarning() {

? ? ? ? super.didReceiveMemoryWarning()

? ? ? ? // Dispose of any resources that can be recreated.

? ? }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容