To create a label and add it to a view controller in Swift, follow these steps:
Open Xcode and create a new project.
Open the Main.storyboard file and select the view controller you want to add the label to.
Drag a UILabel object from the Object Library onto the view controller.
In the Attributes Inspector, set the text, font, color, and any other properties you want for the label.
Adjust the size and position of the label by dragging the blue handles around it.
To add constraints to the label, select it and click the Pin button at the bottom-right of the Interface Builder pane. Choose the constraints you want to add and click Add Constraint.
Connect the label to the view controller code by control-dragging from the label to the view controller code file. Choose Outlet from the list of options and give the outlet a name (e.g. label).
In the view controller code, add the following line of code to viewDidLoad() function:
main.swift23 chars2 lines
This will add the label as a subview to the view controller's view.
That's it! You now have a label added to a view controller in Swift.
gistlibby LogSnag