78

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.

Thorax
  • 2,352
  • 1
  • 22
  • 27

3 Answers3

172

These are now enums.

You can do:

label.textAlignment = NSTextAlignment.center;

Or, for shorthand:

label.textAlignment = .center;

Swift 3

label.textAlignment = .center
Mitesh Khatri
  • 3,935
  • 4
  • 44
  • 67
Cezary Wojcik
  • 21,745
  • 6
  • 36
  • 36
  • Got it! my mistake was that i was writing .center instead – Thorax Jun 04 '14 at 11:09
  • 1
    Could you link to the documentation ? Since as I can see they had to rewrite those enums from ObjC. – Paweł Brewczynski Jun 29 '14 at 20:00
  • This answer helped. I tried .Center and it didn't look right. Checked enums and used .Justified and it looked better enum { NSTextAlignmentLeft = 0, NSTextAlignmentCenter = 1, NSTextAlignmentRight = 2, NSTextAlignmentJustified = 3, NSTextAlignmentNatural = 4, }; – Hblegg Sep 18 '15 at 23:50
2

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

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

yoeriboven
  • 3,541
  • 3
  • 25
  • 40
0

For Centre use, myLabel.textAlignment = NSTextAlignment.Center

Ankit Goyal
  • 3,019
  • 1
  • 21
  • 26