create uilabel in swift

There are two ways to create a UILabel in Swift: through Interface Builder or programmatically.

Interface Builder Approach:

  1. Open your storyboard or xib file.
  2. Drag a UILabel from the Object Library onto your view.
  3. Customize your label using the Attributes Inspector in the Utilities Pane.

Programmatic Approach:

main.swift
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
label.center = CGPoint(x: 160, y: 284)
label.textAlignment = .center
label.text = "Hello, World!"
self.view.addSubview(label)
197 chars
6 lines

In this example, we are initializing a UILabel instance with a frame and adding it to our view as a subview. We then customize the label properties such as textAlignment and text.

gistlibby LogSnag