UITextField

UITextField displays editable text.

Example

UITextField

Instructions

Create

  1. Open Xcode
  2. Click File
  3. Click New
  4. Click Project...
  5. Under iOS, click Application
  6. Click Single View Application
  7. Click Next
  8. After Product Name, type your product name
  9. After Organization Name, type your organization name or leave blank
  10. After Organization Identifier, type your organization identifier
  11. After Language, select Swift
  12. After Devices, select Universal
  13. Uncheck Use Core Data
  14. Click Next
  15. Select a folder
  16. After Source Control, uncheck Create Git repository on
  17. Click Create

Edit ViewController.swift

  1. Open ViewController.swift
  2. Copy and paste code

Edit Main.storyboard

  1. Open Main.storyboard
  2. Click View
  3. Click Utilities
  4. Click Show Object Library
  5. Drag Text Field to View
  6. Click View
  7. Click Utilities
  8. Click Show Connections Inspector
  9. Under Sent Events, after Did End On Exit, drag + to View Controller
  10. Click didEndOnExit:
  11. Under Referencing Outlets, after New Referencing Outlet, drag + to View Controller
  12. Click interfaceBuilderTextField

Add Images

  1. Click File
  2. Click Add Files to "UITextField" ...
  3. Select images
  4. After Destination, check Copy items if needed
  5. After Added folders, select Create groups
  6. After Add to targets, check UITextField
  7. Click Add

Run

  1. Click Product
  2. Click Run

Code

import UIKit

class ViewController: UIViewController
{
  @IBOutlet weak var interfaceBuilderTextField: UITextField!
  
  override func prefersStatusBarHidden() -> Bool
  {
    return true
  }
  override func viewDidLoad()
  {
    super.viewDidLoad()
    
    let image1 = UIImage(named: "HappyFace1")
    let image2 = UIImage(named: "HappyFace2")
    
    self.interfaceBuilderTextField.text = "Interface Builder"
    self.view.removeConstraints(self.view.constraints())

    let adjustFontSizeToFitWidthTextField = UITextField()
    adjustFontSizeToFitWidthTextField.addTarget(self, action: "didEndOnExit:", forControlEvents: UIControlEvents.EditingDidEndOnExit)
    adjustFontSizeToFitWidthTextField.adjustsFontSizeToFitWidth = true
    adjustFontSizeToFitWidthTextField.font = UIFont(name: "Times New Roman", size: 24)
    adjustFontSizeToFitWidthTextField.minimumFontSize = 12
    adjustFontSizeToFitWidthTextField.setTranslatesAutoresizingMaskIntoConstraints(false)
    adjustFontSizeToFitWidthTextField.text = "Adjusts Font Size To Fit Width"
    self.view.addSubview(adjustFontSizeToFitWidthTextField)
    self.view.addConstraint(NSLayoutConstraint(item: adjustFontSizeToFitWidthTextField, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.interfaceBuilderTextField, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: adjustFontSizeToFitWidthTextField, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Width, multiplier: 1/4, constant: 0))

    let allowsEditingTextAttributesTextField = UITextField()
    allowsEditingTextAttributesTextField.addTarget(self, action: "didEndOnExit:", forControlEvents: UIControlEvents.EditingDidEndOnExit)
    allowsEditingTextAttributesTextField.allowsEditingTextAttributes = true
    allowsEditingTextAttributesTextField.setTranslatesAutoresizingMaskIntoConstraints(false)
    allowsEditingTextAttributesTextField.text = "Allows Editing Text Attributes"
    self.view.addSubview(allowsEditingTextAttributesTextField)
    self.view.addConstraint(NSLayoutConstraint(item: allowsEditingTextAttributesTextField, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: adjustFontSizeToFitWidthTextField, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let attributedPlaceholderAttributedString = NSMutableAttributedString(string: "Attributed Placeholder")
    attributedPlaceholderAttributedString.addAttribute(NSBackgroundColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(0, 10))
    attributedPlaceholderAttributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.whiteColor(), range: NSMakeRange(0, 10))
    attributedPlaceholderAttributedString.addAttribute(NSFontAttributeName, value: UIFont(name: "Times New Roman", size: 24)!, range: NSMakeRange(11, 11))
    
    let attributedPlaceholderTextField = UITextField()
    attributedPlaceholderTextField.addTarget(self, action: "didEndOnExit:", forControlEvents: UIControlEvents.EditingDidEndOnExit)
    attributedPlaceholderTextField.attributedPlaceholder = attributedPlaceholderAttributedString
    attributedPlaceholderTextField.setTranslatesAutoresizingMaskIntoConstraints(false)
    self.view.addSubview(attributedPlaceholderTextField)
    self.view.addConstraint(NSLayoutConstraint(item: attributedPlaceholderTextField, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: allowsEditingTextAttributesTextField, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let attributedTextAttributedString = NSMutableAttributedString(string: "Attributed Text")
    attributedTextAttributedString.addAttribute(NSBackgroundColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(0, 10))
    attributedTextAttributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.whiteColor(), range: NSMakeRange(0, 10))
    attributedTextAttributedString.addAttribute(NSFontAttributeName, value: UIFont(name: "Times New Roman", size: 24)!, range: NSMakeRange(11, 4))

    let attributedTextTextField = UITextField()
    attributedTextTextField.addTarget(self, action: "didEndOnExit:", forControlEvents: UIControlEvents.EditingDidEndOnExit)
    attributedTextTextField.attributedText = attributedTextAttributedString
    attributedTextTextField.setTranslatesAutoresizingMaskIntoConstraints(false)
    self.view.addSubview(attributedTextTextField)
    self.view.addConstraint(NSLayoutConstraint(item: attributedTextTextField, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: attributedPlaceholderTextField, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: attributedTextTextField, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))

    let backgroundTextField = UITextField()
    backgroundTextField.addTarget(self, action: "didEndOnExit:", forControlEvents: UIControlEvents.EditingDidEndOnExit)
    backgroundTextField.background = image1
    backgroundTextField.setTranslatesAutoresizingMaskIntoConstraints(false)
    backgroundTextField.text = "Background"
    self.view.addSubview(backgroundTextField)
    self.view.addConstraint(NSLayoutConstraint(item: backgroundTextField, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: attributedTextTextField, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let disabledBackgroundTextField = UITextField()
    disabledBackgroundTextField.addTarget(self, action: "didEndOnExit:", forControlEvents: UIControlEvents.EditingDidEndOnExit)
    disabledBackgroundTextField.background = image1
    disabledBackgroundTextField.disabledBackground = image2
    disabledBackgroundTextField.enabled = false
    disabledBackgroundTextField.setTranslatesAutoresizingMaskIntoConstraints(false)
    disabledBackgroundTextField.text = "Disabled Background"
    self.view.addSubview(disabledBackgroundTextField)
    self.view.addConstraint(NSLayoutConstraint(item: disabledBackgroundTextField, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: backgroundTextField, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let borderStyleBezelTextField = UITextField()
    borderStyleBezelTextField.addTarget(self, action: "didEndOnExit:", forControlEvents: UIControlEvents.EditingDidEndOnExit)
    borderStyleBezelTextField.borderStyle = UITextBorderStyle.Bezel
    borderStyleBezelTextField.setTranslatesAutoresizingMaskIntoConstraints(false)
    borderStyleBezelTextField.text = "Border Style Bezel"
    self.view.addSubview(borderStyleBezelTextField)
    self.view.addConstraint(NSLayoutConstraint(item: borderStyleBezelTextField, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: disabledBackgroundTextField, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let borderStyleLineTextField = UITextField()
    borderStyleLineTextField.addTarget(self, action: "didEndOnExit:", forControlEvents: UIControlEvents.EditingDidEndOnExit)
    borderStyleLineTextField.borderStyle = UITextBorderStyle.Line
    borderStyleLineTextField.setTranslatesAutoresizingMaskIntoConstraints(false)
    borderStyleLineTextField.text = "Border Style Line"
    self.view.addSubview(borderStyleLineTextField)
    self.view.addConstraint(NSLayoutConstraint(item: borderStyleLineTextField, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: borderStyleBezelTextField, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let borderStyleNoneTextField = UITextField()
    borderStyleNoneTextField.addTarget(self, action: "didEndOnExit:", forControlEvents: UIControlEvents.EditingDidEndOnExit)
    borderStyleNoneTextField.borderStyle = UITextBorderStyle.None
    borderStyleNoneTextField.setTranslatesAutoresizingMaskIntoConstraints(false)
    borderStyleNoneTextField.text = "Border Style None"
    self.view.addSubview(borderStyleNoneTextField)
    self.view.addConstraint(NSLayoutConstraint(item: borderStyleNoneTextField, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: borderStyleLineTextField, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let borderStyleRoundedRectTextField = UITextField()
    borderStyleRoundedRectTextField.addTarget(self, action: "didEndOnExit:", forControlEvents: UIControlEvents.EditingDidEndOnExit)
    borderStyleRoundedRectTextField.borderStyle = UITextBorderStyle.RoundedRect
    borderStyleRoundedRectTextField.setTranslatesAutoresizingMaskIntoConstraints(false)
    borderStyleRoundedRectTextField.text = "Border Style Rounded Rect"
    self.view.addSubview(borderStyleRoundedRectTextField)
    self.view.addConstraint(NSLayoutConstraint(item: borderStyleRoundedRectTextField, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: borderStyleNoneTextField, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let clearButtonModeTextField = UITextField()
    clearButtonModeTextField.addTarget(self, action: "didEndOnExit:", forControlEvents: UIControlEvents.EditingDidEndOnExit)
    clearButtonModeTextField.clearButtonMode = UITextFieldViewMode.Always
    clearButtonModeTextField.setTranslatesAutoresizingMaskIntoConstraints(false)
    clearButtonModeTextField.text = "Clear Button Mode"
    self.view.addSubview(clearButtonModeTextField)
    self.view.addConstraint(NSLayoutConstraint(item: clearButtonModeTextField, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: borderStyleRoundedRectTextField, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let clearsOnBeginEditingTextField = UITextField()
    clearsOnBeginEditingTextField.addTarget(self, action: "didEndOnExit:", forControlEvents: UIControlEvents.EditingDidEndOnExit)
    clearsOnBeginEditingTextField.clearsOnBeginEditing = true
    clearsOnBeginEditingTextField.setTranslatesAutoresizingMaskIntoConstraints(false)
    clearsOnBeginEditingTextField.text = "Clears On Begin Editing"
    self.view.addSubview(clearsOnBeginEditingTextField)
    self.view.addConstraint(NSLayoutConstraint(item: clearsOnBeginEditingTextField, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: clearButtonModeTextField, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let clearsOnInsertionTextField = UITextField()
    clearsOnInsertionTextField.addTarget(self, action: "didEndOnExit:", forControlEvents: UIControlEvents.EditingDidEndOnExit)
    clearsOnInsertionTextField.clearsOnInsertion = true
    clearsOnInsertionTextField.setTranslatesAutoresizingMaskIntoConstraints(false)
    clearsOnInsertionTextField.text = "Clears On Insertion"
    self.view.addSubview(clearsOnInsertionTextField)
    self.view.addConstraint(NSLayoutConstraint(item: clearsOnInsertionTextField, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: clearsOnBeginEditingTextField, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let defaultTextAttributesTextField = UITextField()
    defaultTextAttributesTextField.addTarget(self, action: "didEndOnExit:", forControlEvents: UIControlEvents.EditingDidEndOnExit)
    defaultTextAttributesTextField.defaultTextAttributes = NSDictionary(objectsAndKeys: UIFont(name: "Times New Roman", size: 24)!, NSFontAttributeName, UIColor.whiteColor(), NSForegroundColorAttributeName, UIColor.redColor(), NSBackgroundColorAttributeName)
    defaultTextAttributesTextField.setTranslatesAutoresizingMaskIntoConstraints(false)
    defaultTextAttributesTextField.text = "Default Text Attributes"
    self.view.addSubview(defaultTextAttributesTextField)
    self.view.addConstraint(NSLayoutConstraint(item: defaultTextAttributesTextField, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: clearsOnInsertionTextField, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let fontTextField = UITextField()
    fontTextField.addTarget(self, action: "didEndOnExit:", forControlEvents: UIControlEvents.EditingDidEndOnExit)
    fontTextField.font = UIFont(name: "Times New Roman", size: 24)
    fontTextField.setTranslatesAutoresizingMaskIntoConstraints(false)
    fontTextField.text = "Font"
    self.view.addSubview(fontTextField)
    self.view.addConstraint(NSLayoutConstraint(item: fontTextField, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: defaultTextAttributesTextField, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let inputAccessoryViewImageView = UIImageView(image: image1)

    let inputAccessoryViewTextField = UITextField()
    inputAccessoryViewTextField.addTarget(self, action: "didEndOnExit:", forControlEvents: UIControlEvents.EditingDidEndOnExit)
    inputAccessoryViewTextField.inputAccessoryView = inputAccessoryViewImageView
    inputAccessoryViewTextField.setTranslatesAutoresizingMaskIntoConstraints(false)
    inputAccessoryViewTextField.text = "Input Accessory View"
    self.view.addSubview(inputAccessoryViewTextField)
    self.view.addConstraint(NSLayoutConstraint(item: inputAccessoryViewTextField, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: fontTextField, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let inputViewImageView = UIImageView(image: image1)
    
    let inputViewTextField = UITextField()
    inputViewTextField.addTarget(self, action: "didEndOnExit:", forControlEvents: UIControlEvents.EditingDidEndOnExit)
    inputViewTextField.inputView = inputViewImageView
    inputViewTextField.setTranslatesAutoresizingMaskIntoConstraints(false)
    inputViewTextField.text = "Input View"
    self.view.addSubview(inputViewTextField)
    self.view.addConstraint(NSLayoutConstraint(item: inputViewTextField, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: inputAccessoryViewTextField, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let leftViewImageView = UIImageView(image: image1)
    
    let leftViewTextField = UITextField()
    leftViewTextField.addTarget(self, action: "didEndOnExit:", forControlEvents: UIControlEvents.EditingDidEndOnExit)
    leftViewTextField.leftView = leftViewImageView
    leftViewTextField.leftViewMode = UITextFieldViewMode.Always
    leftViewTextField.setTranslatesAutoresizingMaskIntoConstraints(false)
    leftViewTextField.text = "Left View"
    self.view.addSubview(leftViewTextField)
    self.view.addConstraint(NSLayoutConstraint(item: leftViewTextField, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: inputViewTextField, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let rightViewImageView = UIImageView(image: image1)
    
    let rightViewTextField = UITextField()
    rightViewTextField.addTarget(self, action: "didEndOnExit:", forControlEvents: UIControlEvents.EditingDidEndOnExit)
    rightViewTextField.rightView = rightViewImageView
    rightViewTextField.rightViewMode = UITextFieldViewMode.Always
    rightViewTextField.setTranslatesAutoresizingMaskIntoConstraints(false)
    rightViewTextField.text = "Right View"
    self.view.addSubview(rightViewTextField)
    self.view.addConstraint(NSLayoutConstraint(item: rightViewTextField, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: leftViewTextField, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let minimumFontSizeTextField = UITextField()
    minimumFontSizeTextField.addTarget(self, action: "didEndOnExit:", forControlEvents: UIControlEvents.EditingDidEndOnExit)
    minimumFontSizeTextField.adjustsFontSizeToFitWidth = true
    minimumFontSizeTextField.font = UIFont(name: "Times New Roman", size: 24)
    minimumFontSizeTextField.minimumFontSize = 12
    minimumFontSizeTextField.setTranslatesAutoresizingMaskIntoConstraints(false)
    minimumFontSizeTextField.text = "Minimum Font Size"
    self.view.addSubview(minimumFontSizeTextField)
    self.view.addConstraint(NSLayoutConstraint(item: minimumFontSizeTextField, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: rightViewTextField, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: minimumFontSizeTextField, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Width, multiplier: 1/4, constant: 0))

    let placeholderTextField = UITextField()
    placeholderTextField.addTarget(self, action: "didEndOnExit:", forControlEvents: UIControlEvents.EditingDidEndOnExit)
    placeholderTextField.placeholder = "Placeholder"
    placeholderTextField.setTranslatesAutoresizingMaskIntoConstraints(false)
    self.view.addSubview(placeholderTextField)
    self.view.addConstraint(NSLayoutConstraint(item: placeholderTextField, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: minimumFontSizeTextField, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let textTextField = UITextField()
    textTextField.addTarget(self, action: "didEndOnExit:", forControlEvents: UIControlEvents.EditingDidEndOnExit)
    textTextField.setTranslatesAutoresizingMaskIntoConstraints(false)
    textTextField.text = "Text"
    self.view.addSubview(textTextField)
    self.view.addConstraint(NSLayoutConstraint(item: textTextField, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: placeholderTextField, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let textAlignmentTextField = UITextField()
    textAlignmentTextField.addTarget(self, action: "didEndOnExit:", forControlEvents: UIControlEvents.EditingDidEndOnExit)
    textAlignmentTextField.setTranslatesAutoresizingMaskIntoConstraints(false)
    textAlignmentTextField.text = "Text Alignment"
    textAlignmentTextField.textAlignment = NSTextAlignment.Right
    self.view.addSubview(textAlignmentTextField)
    self.view.addConstraint(NSLayoutConstraint(item: textAlignmentTextField, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textTextField, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: textAlignmentTextField, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))

    let textColorTextField = UITextField()
    textColorTextField.addTarget(self, action: "didEndOnExit:", forControlEvents: UIControlEvents.EditingDidEndOnExit)
    textColorTextField.setTranslatesAutoresizingMaskIntoConstraints(false)
    textColorTextField.text = "Text Color"
    textColorTextField.textColor = UIColor.redColor()
    self.view.addSubview(textColorTextField)
    self.view.addConstraint(NSLayoutConstraint(item: textColorTextField, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textAlignmentTextField, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let typingAttributesTextField = UITextField()
    typingAttributesTextField.addTarget(self, action: "didEndOnExit:", forControlEvents: UIControlEvents.EditingDidEndOnExit)
    typingAttributesTextField.addTarget(self, action: "editingDidBegin:", forControlEvents: UIControlEvents.EditingDidBegin)
    typingAttributesTextField.defaultTextAttributes = NSDictionary(objectsAndKeys: UIFont(name: "Times New Roman", size: 24)!, NSFontAttributeName, UIColor.whiteColor(), NSForegroundColorAttributeName, UIColor.redColor(), NSBackgroundColorAttributeName)
    typingAttributesTextField.setTranslatesAutoresizingMaskIntoConstraints(false)
    typingAttributesTextField.text = "Typing Attributes"
    self.view.addSubview(typingAttributesTextField)
    self.view.addConstraint(NSLayoutConstraint(item: typingAttributesTextField, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textColorTextField, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
  }
  @IBAction func didEndOnExit(sender: UITextField!)
  {
    sender.resignFirstResponder()
  }
  func editingDidBegin(sender: UITextField!)
  {
    sender.typingAttributes = NSDictionary(objectsAndKeys: UIFont(name: "Times New Roman", size: 24)!, NSFontAttributeName, UIColor.whiteColor(), NSForegroundColorAttributeName, UIColor.greenColor(), NSBackgroundColorAttributeName)
  }
}

External

Apple iOS Developer Library