UILABEL
UILabel displays static text.
EXAMPLE

INSTRUCTIONS
CREATE
- Open Xcode
- Click File
- Click New
- Click Project...
- Under iOS, click Application
- Click Single View Application
- Click Next
- After Product Name, type your product name
- After Organization Name, type your organization name or leave blank
- After Organization Identifier, type your organization identifier
- After Language, select Swift
- After Devices, select Universal
- Uncheck Use Core Data
- Click Next
- Select a folder
- After Source Control, uncheck Create Git repository on
- Click Create
EDIT VIEWCONTROLLER.SWIFT
- Open ViewController.swift
- Copy and paste code
EDIT MAIN.STORYBOARD
- Open Main.storyboard
- Click View
- Click Utilities
- Click Show Object Library
- Drag Label to View
- Click View
- Click Utilities
- Click Show Connections Inspector
- Under Referencing Outlets, after New Referencing Outlet, drag + to View Controller
- Click interfaceBuilderLabel
RUN
- Click Product
- 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))
}
}