There's probably a ridiculously simple solution to this (I'm almost embarrassed to ask).
Relevant code snippet 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 "Get Time" button, the "Press Button" label is centred:
After pressing the button, the label gets left-alligned:
Label attributes:
I have set the labels constraints, but the constrains seem to get "reset" when the string value is set.
How can I make the label stay centred after setting the string value?
I've also tried to add this to my controller, but to no avail:
let textAlignment = NSTextAlignment.CenterTextAlignment
textLabel.alignment = textAlignment
Try this:
textLabel.alignment = .center
I used this and it worked.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With