淘先锋技术网

首页 1 2 3 4 5 6 7

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)
  }
  
}