【发布时间】: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