0

macOS 开发新手并尝试创建一些非常基本的布局,但是我正在努力将 NSTextFields 添加到位于 NSScrollView 内的 NSStackView,到目前为止,我已经编写了以下内容,但它似乎将文本限制为单行,调整大小适合的窗户......图片

@IBOutlet weak var sv: NSStackView!

let lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    sv.autoresizesSubviews = true
    for i in 0..<10{
        var test = NSTextField(string: lorem)
        test.isEditable = false
        test.isBordered = false
        test.translatesAutoresizingMaskIntoConstraints = false
        test.usesSingleLineMode = false
        test.lineBreakMode = .byCharWrapping
        test.wantsLayer = true
        test.layer?.cornerRadius = 5

        sv.addView(test, in: .top)

    }}
4

1 回答 1

1

用作初始化程序将NSTextField(string:)字段冻结为单行。试试这个方便的初始化器:

let test = NSTextField(wrappingLabelWithString: lorem)

…不要打扰.isEditable, .translatesAutoresizingMaskIntoConstraints, 或.usesSingleLineMode.

于 2020-03-27T01:38:26.733 回答