Swift. UILabel text alignment

Thorax picture Thorax · Jun 4, 2014 · Viewed 82.3k times · Source

I create my UILabel in swift:

let label = UILabel(frame: CGRect( x: 50, y: 50, width: 100, height: 50))

setting properties seems to be easy:

label.textColor = UIColor.redColor()

How to implement enum types like textAlignment? In Objective C it was

label.textAlignment = NSTextAlignmentCenter;

but in swift it doesn't seem to work.

Answer

Cezary Wojcik picture Cezary Wojcik · Jun 4, 2014

These are now enums.

You can do:

label.textAlignment = NSTextAlignment.center;

Or, for shorthand:

label.textAlignment = .center;

Swift 3

label.textAlignment = .center