【问题标题】:Text Direction of NSTextFieldNSTextField 的文本方向
【发布时间】:2020-11-07 02:45:50
【问题描述】:

我想要一个文本方向为的 NSTextField

我怎样才能做到这一点?提前感谢您的帮助。

【问题讨论】:

  • 如果它是静态的,你是否尝试旋转NSTextField
  • 不,我没有尝试旋转
  • 好的,我添加答案@Awais,希望对您有帮助
  • 嘿@Awais,成功了吗?

标签: swift macos nstextfield


【解决方案1】:

选择方向为Vertical,然后设置文本。

输入必须是这样的。

感谢@Willeke 的评论,我错过了NSTextFieldpoints 和NSTextView

Cocoa框架有一些旋转UI组件的点。

为了你的NSTextField

myTextField.rotate(byDegrees: 90)

仔细设置宽度和高度。

【讨论】:

  • NSTextView 而不是 NSTextField?
  • 感谢@Willeke 的评论,我得到NSTextView 所以另外编辑了它
【解决方案2】:

我旋转 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
  }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-18
    相关资源
    最近更新 更多