可能有一个非常简单的解决方案(我几乎不好意思问)。
来 self 的 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)
}
在按下“获取时间”按钮之前,“按下按钮”标签居中:
按下按钮后,标签左对齐:
标签属性:
我已经设置了标签约束,但是当设置字符串值时,约束似乎“重置”了。
如何在设置字符串值后让标签保持居中?
我也试过将它添加到我的 Controller ,但无济于事:
let textAlignment = NSTextAlignment.CenterTextAlignment
textLabel.alignment = textAlignment
最佳答案
试试这个:
textLabel.alignment = .center
我用过这个,效果很好。
关于cocoa - 如何在设置其字符串值后居中对齐 NSTextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27160502/