Create UILabel and Set Attributes in Swift

When working with a UILabel in Swift, all the properties you are familiar with from Objective-C are available, including textAlignment, textColor, lineBreakMode, numberOfLines and userInteractionEnabled (among others).

The code example that follows shows a basic UILabel configuration.

let lbl = UILabel(frame: CGRectMake(0, 0, 300, 200))
lbl.text = ""
// Enum type, two variations:
lbl.textAlignment = NSTextAlignment.Right
lbl.textAlignment = .Right
lbl.textColor = UIColor.redColor()
lbl.shadowColor = UIColor.blackColor()
lbl.font = UIFont(name: "HelveticaNeue", size: CGFloat(22))

Comments are closed.