cocoa - 如何在设置其字符串值后居中对齐 NSTextField

标签 cocoa swift alignment nstextfield

可能有一个非常简单的解决方案(我几乎不好意思问)。

来 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)
}

在按下“获取时间”按钮之前,“按下按钮”标签居中:

enter image description here

按下按钮后,标签左对齐:

enter image description here

标签属性:

enter image description here

我已经设置了标签约束,但是当设置字符串值时,约束似乎“重置”了。

如何在设置字符串值后让标签保持居中?

我也试过将它添加到我的 Controller ,但无济于事:

let textAlignment = NSTextAlignment.CenterTextAlignment
textLabel.alignment = textAlignment

最佳答案

试试这个:

textLabel.alignment = .center

我用过这个,效果很好。

关于cocoa - 如何在设置其字符串值后居中对齐 NSTextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27160502/

相关文章:

macos - 使用 NSPopUpButtonCell 时在 NSTableView 中获取重复的标题按钮单元格

objective-c - 返回可变的值,而返回值应该是不可变的

delphi - 如何以编程方式完全证明文本合理(Delphi)?

objective-c - 使用 NSXMLParser delegate 解析两个文件

objective-c - Objective-C 中的简单套接字

ios - 如何将 Firebase 数据库持久性存储限制为仅某些节点?

ios - Swift 'WKInterfaceLabel' 没有名为 'text' 的成员

xcode - 如何将语言添加到 NSSpellChecker 的可用语言中?

ios - 如何在应用程序加载时首先显示 3 个 View 中的 View - IOS

C++ 对齐(何时使用 alignas)