【问题标题】:NSTextView with NSTextTable: Set minimum width with horizontal scrolling?带有 NSTextTable 的 NSTextView:使用水平滚动设置最小宽度?
【发布时间】:2020-07-31 11:05:54
【问题描述】:

我有一个带有NSTextView 的 Cocoa 应用程序。我展示了一个相当大的表格(通过NSAttributedStringNSTextTableNSTextTableBlock 制作)。

我想实现以下行为:

  1. 让表格在文本视图的整个宽度上展开,每一列的宽度相同
  2. 设置表格/文本视图的最小宽度,以防止在用户调整窗口大小并使其变得非常小时时表格被挤压在一起;改为显示水平滚动条。

我设法让#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)

我还尝试为文本视图设置自动布局约束,但这仅强制执行 minWidth,不显示水平滚动条。

【问题讨论】:

    标签: cocoa autolayout nsattributedstring nstextview nsscrollview


    【解决方案1】:

    其他内容呢?喜欢表格上方/下方的文字?收缩?保持与桌子相同的最小宽度?我的假设与表格的宽度相同。

    以下示例不是完整的答案,请将其视为您必须开始的概念验证。

    屏幕录制

    对于 GIF 质量很抱歉,但限制为 2MB。

    示例项目

    • 在 IB 中添加了 Text View 并将其连接到插座
    • 其余在代码中完成

    重要部分

    滚动条

    同时设置它们并让它们根据内容自动隐藏。

    scrollView.hasVerticalScroller = true
    scrollView.hasHorizontalScroller = true
    scrollView.autohidesScrollers = true
    

    切换文本视图调整大小和设置容器大小

    由于viewDidLayout 覆盖等原因,这是一个蹩脚的实现。例如 - 如果您的鼠标和窗口大小调整速度足够快,则最终的尺寸可能远小于 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)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2010-12-15
      • 2014-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-10
      • 1970-01-01
      • 2014-01-25
      相关资源
      最近更新 更多