5
Description
Add a label.
The label displays static text.
Example
Instructions
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 supportedInterfaceOrientations() -> Int
{
return Int(UIInterfaceOrientationMask.All.rawValue)
}
override func viewDidLoad()
{
super.viewDidLoad()
self.interfaceBuilderLabel.backgroundColor = UIColor.blackColor()
self.interfaceBuilderLabel.text = "Hello Xcode"
self.interfaceBuilderLabel.textAlignment = NSTextAlignment.Center
self.interfaceBuilderLabel.textColor = UIColor.whiteColor()
self.view.removeConstraints(self.view.constraints())
self.view.addConstraint(NSLayoutConstraint(item: self.interfaceBuilderLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 20))
self.view.addConstraint(NSLayoutConstraint(item: self.interfaceBuilderLabel, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 20))
self.view.addConstraint(NSLayoutConstraint(item: self.interfaceBuilderLabel, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: -40))
self.view.addConstraint(NSLayoutConstraint(item: self.interfaceBuilderLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: -40))
}
}