create label in swift

To create a label in Swift, you can follow these steps:

  1. Open Xcode and create a new project.
  2. Drag and drop a UILabel object from the object library onto your view controller.
  3. Select the label and use the attributes inspector to customize its appearance, such as font, color, and alignment.
  4. Create an IBOutlet to the label in your view controller file, which will allow you to access and modify the label's properties in code.

Here is an example of how to create a label and set its text programmatically:

main.swift
let myLabel = UILabel(frame: CGRect(x: 50, y: 50, width: 200, height: 50))
myLabel.text = "Hello, world!"
view.addSubview(myLabel)
131 chars
4 lines

This creates a new UILabel object with a frame of (50, 50, 200, 50) and sets its text to "Hello, world!". Finally, it adds the label to the current view as a subview.

related categories

gistlibby LogSnag