Swift. UILabel text alignment

EnumsUilabelSwift

Enums Problem Overview


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.

Enums Solutions


Solution 1 - Enums

These are now enums.

You can do:

label.textAlignment = NSTextAlignment.center;

Or, for shorthand:

label.textAlignment = .center;

Swift 3

label.textAlignment = .center

Solution 2 - Enums

Enums in Swift are different than they are in Objective-C.

What in Objective-C would be NSTextAlignmentCenter is in Swift NSTextAlignment.Center.

Solution 3 - Enums

For Centre use, myLabel.textAlignment = NSTextAlignment.Center

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionThoraxView Question on Stackoverflow
Solution 1 - EnumsCezary WojcikView Answer on Stackoverflow
Solution 2 - EnumsyoeribovenView Answer on Stackoverflow
Solution 3 - EnumsAnkit GoyalView Answer on Stackoverflow