Swift文本输入框(UITextField)的用法

import UIKit

class ViewController: UIViewController        ,UITextFieldDelegate,UITextViewDelegate,UIActionSheet  Delegate,UIAlertViewDelegate{
override func viewDidLoad() {
    super.viewDidLoad()
    
   let textField:UITextField = UITextField(frame: CGRectMake(10,120,200,30))
    
    textField.borderStyle = UITextBorderStyle.RoundedRect
    
    textField.placeholder = "请输入用户名"
    
    //文字大小超过文本框长度时自动缩小字号,而不是隐藏显示省略号
    textField.adjustsFontSizeToFitWidth = true//当文字超出文本框宽度时,自动调整文字大小
    textField.minimumFontSize = 14//最小可缩小的字号
    
    textField.textAlignment = NSTextAlignment.Right
    
// textField.co  ntentVerticalAlignment = .Bottom
    textField.delegate = self;
    
    self.view.addSubview(textField)
    
//textField.borderStyle = .None
    
//textField.background = UIImage(named: "lalala")
    textField.clearButtonMode = UITextFieldViewMode.Always
   
    
    textField.returnKeyType = UIReturnKeyType.Done
 }



func textFieldShouldReturn(textField:UITextField) ->Bool {
    //使文本框在界面打开时就获取焦点,并弹出输入键盘
    textField.resignFirstResponder()
    
    print(textField.text)
    return true
}
}

推荐阅读更多精彩内容