我有一个带有NSTextView
的可可应用。我显示了一个很大的表(通过NSAttributedString
,NSTextTable
和NSTextTableBlock
制成)。
我想要实现以下行为:
我设法使#1正常工作或使#2正常工作,但是我无法弄清楚如何使文本视图的最小宽度同时使表格在整个宽度上伸展。
我的代码:
最小宽度,可滚动,但不能扩展到整个宽度:
self.textView.textStorage?.setAttributedString(tableString)
self.textView.textContainer?.widthTracksTextView = false
self.textView.isHorizontallyResizable = true
cellBlock.setValue(200, type: .absoluteValueType, for: .minimumWidth)
扩展为整个宽度,但未强制使用最小宽度:
self.textView.isHorizontallyResizable = true
cellBlock.setValue(relativeWidth, type: .percentageValueType, for: .width)
我还尝试为文本视图设置Autolayout约束,但这仅强制使用minWidth,而不显示水平滚动条。
答案 0 :(得分:0)
其他内容呢?喜欢表格上方/下方的文字吗?收缩?保持与桌子相同的最小宽度?我的假设是与桌子的宽度相同。
下面的示例不是一个完整的答案,请将其视为您必须首先进行的概念验证。
很抱歉,GIF的质量为2MB。
同时设置它们并根据内容自动隐藏。
scrollView.hasVerticalScroller = true
scrollView.hasHorizontalScroller = true
scrollView.autohidesScrollers = true
由于viewDidLayout
覆盖等原因,这是一个la脚的实现。例如,如果您调整鼠标和窗口大小的速度足够快,那么最终的大小可能会小于300,等等。
这里的重要部分是我更改了哪些属性,并在达到阈值时坚持使用固定的containerSize
(水平)。
if textView.bounds.size.width > 300 {
textView.isHorizontallyResizable = false
textView.textContainer?.widthTracksTextView = true
} else {
textView.textContainer?.widthTracksTextView = false
textView.isHorizontallyResizable = true
textView.textContainer?.containerSize = NSSize(width: textView.bounds.size.width, height: CGFloat.greatestFiniteMagnitude)
}
import Cocoa
class ViewController: NSViewController {
@IBOutlet var scrollView: NSScrollView!
@IBOutlet var textView: NSTextView!
let loremIpsum = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.\n"
let loremIpsumCell = "Lorem Ipsum is simply dummy text of the printing and typesetting industry.\n"
override func viewDidLayout() {
super.viewDidLayout()
if textView.bounds.size.width > 300 {
textView.isHorizontallyResizable = false
textView.textContainer?.widthTracksTextView = true
} else {
textView.textContainer?.widthTracksTextView = false
textView.isHorizontallyResizable = true
textView.textContainer?.containerSize = NSSize(width: textView.bounds.size.width, height: CGFloat.greatestFiniteMagnitude)
}
}
override func viewDidLoad() {
super.viewDidLoad()
scrollView.hasVerticalScroller = true
scrollView.hasHorizontalScroller = true
scrollView.autohidesScrollers = true
let content = NSMutableAttributedString(string: loremIpsum)
let table = NSTextTable()
table.numberOfColumns = 2
table.setContentWidth(100, type: .percentageValueType)
(0...1).forEach { row in
(0...5).forEach { column in
let block = NSTextTableBlock(table: table, startingRow: row, rowSpan: 1, startingColumn: column, columnSpan: 1)
block.setWidth(1.0, type: .absoluteValueType, for: .border)
block.setBorderColor(.orange)
let paragraph = NSMutableParagraphStyle()
paragraph.textBlocks = [block]
let cell = NSMutableAttributedString(string: loremIpsumCell, attributes: [.paragraphStyle: paragraph])
content.append(cell)
}
}
content.append(NSAttributedString(string: loremIpsum))
textView.textStorage?.setAttributedString(content)
}
}