How to center the alignment of an NSTextField after setting its string value

This is probably a ridiculously simple solution (I'm almost embarrassed to ask).

Corresponding piece of code from my view controller:

@IBOutlet weak var textLabel: NSTextField! @IBAction func getTimeButton(sender: AnyObject) { let currentDate = NSDate() let dateFormatter = NSDateFormatter() dateFormatter.timeStyle = .ShortStyle textLabel.stringValue = dateFormatter.stringFromDate(currentDate) } 

Before pressing the button "Get time" the label "Press the button" will be centered:

enter image description here

After pressing the button, the label becomes left:

enter image description here

Label Attributes:

enter image description here

I set label restrictions, but the restrictions seem to get "reset" when a string value is given.

How to make the shortcut remain centered after setting the string value?

I also tried adding this to my controller, but to no avail:

 let textAlignment = NSTextAlignment.CenterTextAlignment textLabel.alignment = textAlignment 
+7
source share
1 answer

Try it:

textLabel.alignment = .center

I used it and it worked.

+12
source

Source: https://habr.com/ru/post/978766/


All Articles