【问题标题】:Setting inset on NSTextView在 NSTextView 上设置插图
【发布时间】:2021-04-28 23:41:45
【问题描述】:

我在滚动视图中有一个文本视图设置(使用代码而不是情节提要)。

// 1. Text Storage
    let attrs = Style.body.attributes
    let attrString = NSAttributedString(string: text, attributes: attrs)
    textStorage = NSTextStorage(attributedString: attrString)
    
    // 2. Layout manager.
    layoutManager = LayoutManager()
    textStorage.addLayoutManager(layoutManager)
    layoutManager.delegate = self
    
    // 3. Layout Manager
    let containerSize = CGSize(width: self.bounds.width, height: .greatestFiniteMagnitude)
    let textContainer = NSTextContainer(size: containerSize)
    textContainer.widthTracksTextView = true
    layoutManager.addTextContainer(textContainer)
    
    // 4. TextView
    textView = NSTextView(frame: self.bounds, textContainer: textContainer)
    textView.delegate = self
    textView.typingAttributes = Style.body.attributes
    
    // 5. ScrollView
    scrollView = NSScrollView()
    scrollView.borderType = .noBorder
    scrollView.hasVerticalScroller = true
    scrollView.backgroundColor = .clear
    scrollView.drawsBackground = false
    scrollView.contentView.backgroundColor = .clear
    scrollView.translatesAutoresizingMaskIntoConstraints = false
    scrollView.borderType = .noBorder
    scrollView.hasVerticalScroller = true
    scrollView.documentView = textView
    
    // 6. Layout and sizing
    textView.minSize = NSSize(width: 0.0, height: scrollView.contentSize.height)
    textView.maxSize = NSSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)
    textView.isVerticallyResizable   = true
    textView.isHorizontallyResizable = false
    textView.autoresizingMask = .width 
    

我想在文本视图周围添加一个插图,这样滚动视图栏就不会覆盖我的文本。最好的方法是什么?

【问题讨论】:

  • 选择滚动视图并查看大小检查器下的设置。
  • @ElTomato 我没有为这个视图使用故事板。那么你将如何只使用代码来做到这一点呢?
  • 请参阅NSScrollView 的文档。

标签: swift nstextview nsscrollview


【解决方案1】:

最后,我用了两个属性来实现一个 inset。

textView.textContainer?.lineFragmentPadding = 20
textView.textContainerInset = NSSize(width: 0, height: 20)

1/ lineFragmentPadding

这会创建水平填充,但不会创建垂直填充

2/ textContainerInset

这会创建两种填充,但如果将其用于水平填充,则会在水平填充区域内出现奇怪的行为。也就是说,光标是一个文本插入光标,当您单击它时不会选择文本或移动光标插入点。

希望对遇到同样问题的人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-15
    相关资源
    最近更新 更多