【发布时间】:2020-11-07 02:45:50
【问题描述】:
【问题讨论】:
-
如果它是静态的,你是否尝试旋转
NSTextField? -
不,我没有尝试旋转
-
好的,我添加答案@Awais,希望对您有帮助
-
嘿@Awais,成功了吗?
标签: swift macos nstextfield
【问题讨论】:
NSTextField?
标签: swift macos nstextfield
我旋转 NSTextField
nameTF.rotate(byDegrees: 270)
计算 NSTextField 的新高度和宽度
let newHeight = nameTF.bestHeight(for: nameTF.stringValue, width: nameTF.frame.width)
let newWidth = nameTF.bestWidth(for: nameTF.stringValue, height: nameTF.frame.height)
设置NSTextField的新框架
nameTF.frame = CGRect(x: nameTF.frame.origin.x, y: self.view.frame.height - newWidth - 30, width: newHeight, height: newWidth)
使用了 NSTextField 的扩展
extension NSTextField {
func bestHeight(for text: String, width: CGFloat) -> CGFloat {
stringValue = text
let height = cell!.cellSize(forBounds: NSRect(x: 0, y: 0, width: width, height: .greatestFiniteMagnitude)).height
return height
}
func bestWidth(for text: String, height: CGFloat) -> CGFloat {
stringValue = text
let width = cell!.cellSize(forBounds: NSRect(x: 0, y: 0, width: .greatestFiniteMagnitude, height: height)).width
return width
}
}
【讨论】: