UITextField和UITextView

UITextField


基本功能


import UIKit

class ViewController: UIViewController {
  
  var textField: UITextField!
  
  override func viewDidLoad() {
    super.viewDidLoad()
    
    textField = UITextField(frame: CGRect(x: 0, y: 0, width: 200, height: 31))

    textField.borderStyle = .RoundedRect
    
    textField.contentVerticalAlignment = .Center
    
    textField.textAlignment = .Center
    
    textField.text = "Sir Richard Branson"
    textField.center = view.center
    view.addSubview(textField)
    
  }
  
}

效果如下:






使用代理


下面的例子通过代理实现了一个统计字符数的功能,并且实时更新。

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

  var textField: UITextField!
  var label: UILabel!
  
  func calculateAndDisplayTextFieldLengthWithText(text: String){
    
    var characterOrCharacters = "Character"
    if text.characters.count != 1{
      characterOrCharacters += "s"
    }
    
    let stringLength = text.characters.count
    
    label.text = "\(stringLength) \(characterOrCharacters)"
    
  }
  
  
  func textField(paramTextField: UITextField,
    shouldChangeCharactersInRange range: NSRange,
    replacementString string: String) -> Bool{
      
      guard let textFieldText = paramTextField.text else {
        return false
      }
      
      let text = NSString(string: textFieldText)
      
      let wholeText =
      text.stringByReplacingCharactersInRange(
        range, withString: string)
      
      calculateAndDisplayTextFieldLengthWithText(wholeText)
      
      return true
      
  }
  
  func textFieldShouldReturn(paramTextField: UITextField) -> Bool{
    paramTextField.resignFirstResponder()
    return true
  }

  override func viewDidLoad() {
    super.viewDidLoad()
    
    textField = UITextField(frame:
      CGRect(x: 38, y: 30, width: 220, height: 31))

    textField.delegate = self
    textField.borderStyle = .RoundedRect
    textField.contentVerticalAlignment = .Center
    textField.textAlignment = .Center
    textField.text = "Sir Richard Branson"
    view.addSubview(textField)
    
    label = UILabel(frame: CGRect(x: 38, y: 61, width: 220, height: 31))
    view.addSubview(label)
    calculateAndDisplayTextFieldLengthWithText(textField.text!)
    
  }

}

效果如下:






UITextView


基本功能


import UIKit

class ViewController: UIViewController {
  
  var textView: UITextView?
  
  override func viewDidLoad() {
    super.viewDidLoad()
    
    textView = UITextView(frame: view.bounds)
    if let theTextView = textView{
      theTextView.text = "Some text goes here..."
      
      theTextView.contentInset =
        UIEdgeInsets(top: 10, left: 0, bottom: 0, right: 0)
      
      theTextView.font = UIFont.systemFontOfSize(16)
      view.addSubview(theTextView)
    }
    
  }
  
}


效果如下:






当点击文字时键盘会自动弹出,占了大概一半的空间。如果用户要输入大量的文字,键盘则会阻挡用户的输入。

下面的例子实现了这样的功能,当用户输到最后一行时,自动加载新的空行。

import UIKit

class ViewController: UIViewController {
  
  var textView: UITextView?
  
  let defaultContentInset =
  UIEdgeInsets(top: 10, left: 0, bottom: 0, right: 0)
  
  func handleKeyboardDidShow (notification: NSNotification){
  
    /* Get the frame of the keyboard */
    let keyboardRectAsObject =
    notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue
    
    /* Place it in a CGRect */
    var keyboardRect = CGRectZero
    
    keyboardRectAsObject.getValue(&keyboardRect)
  
    /* Give a bottom margin to our text view that makes it
    reach to the top of the keyboard */
    textView!.contentInset =
      UIEdgeInsets(top: defaultContentInset.top,
        left: 0, bottom: keyboardRect.height, right: 0)
  }
  
  func handleKeyboardWillHide(notification: NSNotification){
    textView!.contentInset = defaultContentInset
  }
  
  override func viewDidLoad() {
    super.viewDidLoad()
    
    textView = UITextView(frame: view.bounds)
    if let theTextView = textView{
      theTextView.text = "Some text goes here..."
      theTextView.font = UIFont.systemFontOfSize(16)
      theTextView.contentInset = defaultContentInset
      
      view.addSubview(theTextView)
      
      NSNotificationCenter.defaultCenter().addObserver(
        self,
        selector: "handleKeyboardDidShow:",
        name: UIKeyboardDidShowNotification,
        object: nil)
      
      
      NSNotificationCenter.defaultCenter().addObserver(
        self,
        selector: "handleKeyboardWillHide:",
        name: UIKeyboardWillHideNotification,
        object: nil)
      
    }
    
  }
  
  deinit{
    NSNotificationCenter.defaultCenter().removeObserver(self)
  }
  
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值