macOS NSTextField,NSStackView,NSScrollView 如何实现可滚动列表中的换行文本?
macOS NSTextField,NSStackView,NSScrollView How to achieve wrapped text in a scrollable list?
macOS 开发的新手并尝试创建一些非常基本的布局,但是我正在努力将 NSTextFields 添加到位于 NSScrollView 内的 NSStackView,到目前为止我已经写了以下内容但是它似乎将文本限制为单行,调整 window 的大小以适应....
@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)
}}
使用 NSTextField(string:)
作为初始值设定项将字段冻结为一行。试试这个便利的初始值设定项:
let test = NSTextField(wrappingLabelWithString: lorem)
…不要理会 .isEditable
、.translatesAutoresizingMaskIntoConstraints
或 .usesSingleLineMode
。
macOS 开发的新手并尝试创建一些非常基本的布局,但是我正在努力将 NSTextFields 添加到位于 NSScrollView 内的 NSStackView,到目前为止我已经写了以下内容但是它似乎将文本限制为单行,调整 window 的大小以适应....
@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)
}}
使用 NSTextField(string:)
作为初始值设定项将字段冻结为一行。试试这个便利的初始值设定项:
let test = NSTextField(wrappingLabelWithString: lorem)
…不要理会 .isEditable
、.translatesAutoresizingMaskIntoConstraints
或 .usesSingleLineMode
。