Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift. UILabel text alignment

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.

like image 288
Thorax Avatar asked Jun 04 '14 09:06

Thorax


People also ask

How do I align text in a label in Swift?

You can change label text, label text color, label background color, label border width, label border color by setting it's properties. You can also change the label text alignment by change the UILabel object's textAlignment property value t0 NSTextAlignment. left , NSTextAlignment. center or NSTextAlignment.


1 Answers

These are now enums.

You can do:

label.textAlignment = NSTextAlignment.center; 

Or, for shorthand:

label.textAlignment = .center; 

Swift 3

label.textAlignment = .center 
like image 162
Cezary Wojcik Avatar answered Sep 29 '22 16:09

Cezary Wojcik