UILabel

UILabel displays static text.

Example

UILabel

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 Label to View
  6. Click View
  7. Click Utilities
  8. Click Show Connections Inspector
  9. Under Referencing Outlets, after New Referencing Outlet, drag + to View Controller
  10. Click interfaceBuilderLabel

Run

  1. Click Product
  2. Click Run

Code

import UIKit

class ViewController: UIViewController
{
  @IBOutlet weak var interfaceBuilderLabel: UILabel!

  override func prefersStatusBarHidden() -> Bool
  {
    return true
  }
  override func viewDidLoad()
  {
    super.viewDidLoad()
    
    self.interfaceBuilderLabel.text = "Interface Builder"
    self.view.removeConstraints(self.view.constraints())
    
    let adjustsFontSizeToFitWidthLabel = UILabel()
    adjustsFontSizeToFitWidthLabel.adjustsFontSizeToFitWidth = true
    adjustsFontSizeToFitWidthLabel.numberOfLines = 1
    adjustsFontSizeToFitWidthLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
    adjustsFontSizeToFitWidthLabel.text = "Adjusts Font Size To Fit Width"
    self.view.addSubview(adjustsFontSizeToFitWidthLabel)
    self.view.addConstraint(NSLayoutConstraint(item: adjustsFontSizeToFitWidthLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.interfaceBuilderLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: adjustsFontSizeToFitWidthLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Width, multiplier: 1/4, constant: 0))

    let attributedString = NSMutableAttributedString(string: "Attributed Text")
    attributedString.addAttribute(NSBackgroundColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(0, 10))
    attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.whiteColor(), range: NSMakeRange(0, 10))
    attributedString.addAttribute(NSFontAttributeName, value: UIFont(name: "Times New Roman", size: 24)!, range: NSMakeRange(11, 4))
    
    let attributedTextLabel = UILabel()
    attributedTextLabel.attributedText = attributedString;
    attributedTextLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
    self.view.addSubview(attributedTextLabel)
    self.view.addConstraint(NSLayoutConstraint(item: attributedTextLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: adjustsFontSizeToFitWidthLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    
    let baselineAdjustmentLabel = UILabel()
    baselineAdjustmentLabel.adjustsFontSizeToFitWidth = true
    baselineAdjustmentLabel.baselineAdjustment = UIBaselineAdjustment.AlignCenters;
    baselineAdjustmentLabel.numberOfLines = 1
    baselineAdjustmentLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
    baselineAdjustmentLabel.text = "Baseline Adjustment"
    self.view.addSubview(baselineAdjustmentLabel)
    self.view.addConstraint(NSLayoutConstraint(item: baselineAdjustmentLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: attributedTextLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: baselineAdjustmentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Width, multiplier: 1/4, constant: 0))

    let enabledLabel = UILabel()
    enabledLabel.enabled = false
    enabledLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
    enabledLabel.text = "Disabled"
    self.view.addSubview(enabledLabel)
    self.view.addConstraint(NSLayoutConstraint(item: enabledLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: baselineAdjustmentLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let fontLabel = UILabel()
    fontLabel.font = UIFont(name: "Times New Roman", size: 24)
    fontLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
    fontLabel.text = "Font"
    self.view.addSubview(fontLabel)
    self.view.addConstraint(NSLayoutConstraint(item: fontLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: enabledLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let highlightedLabel = UILabel()
    highlightedLabel.highlighted = true
    highlightedLabel.highlightedTextColor = UIColor.redColor()
    highlightedLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
    highlightedLabel.text = "Highlighted"
    self.view.addSubview(highlightedLabel)
    self.view.addConstraint(NSLayoutConstraint(item: highlightedLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: fontLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
  
    let highlightedTextColorLabel = UILabel()
    highlightedTextColorLabel.highlighted = true
    highlightedTextColorLabel.highlightedTextColor = UIColor.redColor()
    highlightedTextColorLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
    highlightedTextColorLabel.text = "Highlighted Text Color"
    self.view.addSubview(highlightedTextColorLabel)
    self.view.addConstraint(NSLayoutConstraint(item: highlightedTextColorLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: highlightedLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
  
    let lineBreakModeLabel = UILabel()
    lineBreakModeLabel.lineBreakMode = NSLineBreakMode.ByTruncatingMiddle;
    lineBreakModeLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
    lineBreakModeLabel.text = "Line Break Mode"
    self.view.addSubview(lineBreakModeLabel)
    self.view.addConstraint(NSLayoutConstraint(item: lineBreakModeLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: highlightedTextColorLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: lineBreakModeLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Width, multiplier: 1/4, constant: 0))

    let minimumScaleFactorLabel = UILabel()
    minimumScaleFactorLabel.adjustsFontSizeToFitWidth = true
    minimumScaleFactorLabel.minimumScaleFactor = 0.8
    minimumScaleFactorLabel.numberOfLines = 1
    minimumScaleFactorLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
    minimumScaleFactorLabel.text = "Minimum Scale Factor"
    self.view.addSubview(minimumScaleFactorLabel)
    self.view.addConstraint(NSLayoutConstraint(item: minimumScaleFactorLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: lineBreakModeLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: minimumScaleFactorLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Width, multiplier: 1/4, constant: 0))

    let numberOfLinesLabel = UILabel()
    numberOfLinesLabel.numberOfLines = 2
    numberOfLinesLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
    numberOfLinesLabel.text = "Number Of Lines Number Of Lines Number Of Lines Number Of Lines Number Of Lines Number Of Lines Number Of Lines Number Of Lines Number Of Lines Number Of Lines Number Of Lines Number Of Lines Number Of Lines Number Of Lines Number Of Lines Number Of Lines Number Of Lines Number Of Lines Number Of Lines Number Of Lines Number Of Lines Number Of Lines Number Of Lines Number Of Lines Number Of Lines Number Of Lines Number Of Lines Number Of Lines Number Of Lines Number Of Lines Number Of Lines Number Of Lines"
    self.view.addSubview(numberOfLinesLabel)
    self.view.addConstraint(NSLayoutConstraint(item: numberOfLinesLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: minimumScaleFactorLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: numberOfLinesLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
    
    let preferredMaxLayoutWidthLabel = UILabel()
    preferredMaxLayoutWidthLabel.numberOfLines = 0
    preferredMaxLayoutWidthLabel.preferredMaxLayoutWidth = 200
    preferredMaxLayoutWidthLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
    preferredMaxLayoutWidthLabel.text = "Preferred Max Layout Width"
    self.view.addSubview(preferredMaxLayoutWidthLabel)
    self.view.addConstraint(NSLayoutConstraint(item: preferredMaxLayoutWidthLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: numberOfLinesLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    
    let shadowColorLabel = UILabel()
    shadowColorLabel.shadowColor = UIColor.redColor()
    shadowColorLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
    shadowColorLabel.text = "Shadow Color"
    self.view.addSubview(shadowColorLabel)
    self.view.addConstraint(NSLayoutConstraint(item: shadowColorLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: preferredMaxLayoutWidthLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    
    let shadowOffsetLabel = UILabel()
    shadowOffsetLabel.shadowColor = UIColor.redColor()
    shadowOffsetLabel.shadowOffset = CGSizeMake(3, 3)
    shadowOffsetLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
    shadowOffsetLabel.text = "Shadow Offset"
    self.view.addSubview(shadowOffsetLabel)
    self.view.addConstraint(NSLayoutConstraint(item: shadowOffsetLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: shadowColorLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    
    let textLabel = UILabel()
    textLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
    textLabel.text = "Text"
    self.view.addSubview(textLabel)
    self.view.addConstraint(NSLayoutConstraint(item: textLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: shadowOffsetLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    
    let textAlignmentLabel = UILabel()
    textAlignmentLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
    textAlignmentLabel.text = "Text Alignment"
    textAlignmentLabel.textAlignment = NSTextAlignment.Right;
    self.view.addSubview(textAlignmentLabel)
    self.view.addConstraint(NSLayoutConstraint(item: textAlignmentLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: textAlignmentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
    
    let textColorLabel = UILabel()
    textColorLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
    textColorLabel.text = "Text Color"
    textColorLabel.textColor = UIColor.redColor()
    self.view.addSubview(textColorLabel)
    self.view.addConstraint(NSLayoutConstraint(item: textColorLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textAlignmentLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
  }
}

External

Apple iOS Developer Library