Hi, I’m pretty new to AppKit and I’m trying to make an NSTextField inside an NSTableView both:
Editable
Multi-line / wrapping
Right now, wrapping works fine until I set:
tf.isEditable = true
Then the text becomes a single line.
How do I make it editable while still wrapping correctly?
import AppKit
final class ViewController: NSViewController, NSTableViewDataSource, NSTableViewDelegate {
let tableView = NSTableView()
let text = String(repeating: "A", count: 500)
override func viewDidLoad() {
super.viewDidLoad()
view = tableView
tableView.addTableColumn(NSTableColumn())
tableView.usesAutomaticRowHeights = true
tableView.dataSource = self
tableView.delegate = self
}
func numberOfRows(in tableView: NSTableView) -> Int { 1 }
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
let cell = NSTableCellView()
let tf = NSTextField(wrappingLabelWithString: text)
tf.lineBreakMode = .byCharWrapping
if let tableColumn {
tf.preferredMaxLayoutWidth = tableColumn.width
}
tf.isEditable = true // comment this out and wrapping works
cell.addSubview(tf)
tf.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
tf.leadingAnchor.constraint(equalTo: cell.leadingAnchor),
tf.trailingAnchor.constraint(equalTo: cell.trailingAnchor),
tf.topAnchor.constraint(equalTo: cell.topAnchor),
tf.bottomAnchor.constraint(equalTo: cell.bottomAnchor),
])
return cell
}
}
3
0
72