【问题标题】:Displaying text in a NSScrollView (Swift)在 NSScrollView (Swift) 中显示文本
【发布时间】:2014-08-07 09:33:08
【问题描述】:
刚开始学习 Swift 并创建了一个小的 macOS 应用程序,我想在其中使用 NSScrollView 来显示属性字符串。我试过了:
@IBOutlet var ScrollViewOutlet : NSScrollView
var attributedString = NSMutableAttributedString(string: myText)
ScrollViewOutlet.insertText(attributedString)
但这似乎不起作用。感觉很困惑,因为用 NSTextView 做同样的事情反而像魅力一样。
我在这里错过了什么?
【问题讨论】:
标签:
xcode
swift
nsscrollview
nsmutableattributedstring
【解决方案1】:
在下面的示例中,我在界面构建器中添加了滚动视图作为我的起点。
如果您的scrollView/textView 为空/空白,或者如果您需要在scrollView/textView 中已有的内容前面附加文本,则以下方法有效。如果框中已有文本,则将新文本插入现有文本的前面。
documentView 是一个 NSTextView
Swift 4.0
@IBOutlet weak var imageDestinationDirectory: NSScrollView!
...
let destinationText = "Text to display"
imageDestinationDirectory.documentView!.insertText(destinationText)
【解决方案3】:
Apple 有一篇关于以编程方式在滚动视图中设置文本视图的文章。 Text System User Interface Layer Programming Guide: Putting an NSTextView Object in an NSScrollView 你应该阅读它以获得最大的理解,但这里是代码:
NSScrollView *scrollview = [[NSScrollView alloc] initWithFrame:[[theWindow contentView] frame]];
NSSize contentSize = [scrollview contentSize];
[scrollview setBorderType:NSNoBorder];
[scrollview setHasVerticalScroller:YES];
[scrollview setHasHorizontalScroller:NO];
[scrollview setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
theTextView = [[NSTextView alloc] initWithFrame:NSMakeRect(0, 0, contentSize.width, contentSize.height)];
[theTextView setMinSize:NSMakeSize(0.0, contentSize.height)];
[theTextView setMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];
[theTextView setVerticallyResizable:YES];
[theTextView setHorizontallyResizable:NO];
[theTextView setAutoresizingMask:NSViewWidthSizable];
[[theTextView textContainer] setContainerSize:NSMakeSize(contentSize.width, FLT_MAX)];
[[theTextView textContainer] setWidthTracksTextView:YES];
[scrollview setDocumentView:theTextView];
[theWindow setContentView:scrollview];
[theWindow makeKeyAndOrderFront:nil];
[theWindow makeFirstResponder:theTextView];
然后您可以通过对其textStorage 对象进行操作来设置文本视图的文本:
theTextView.textStorage.attributedString = attributedString;
【解决方案4】:
以下是如何以编程方式使用自动布局:
class ViewController: NSViewController {
let scrollView = NSScrollView()
let textView = NSTextView()
override func viewDidLoad() {
super.viewDidLoad()
textView.maxSize = NSSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)
textView.autoresizingMask = .width
textView.isVerticallyResizable = true
textView.textContainer?.widthTracksTextView = true
view.addSubview(scrollView)
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.documentView = textView
NSLayoutConstraint.activate([
scrollView.topAnchor.constraint(equalTo: view.topAnchor),
scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
])
}
}
【解决方案5】:
@IBOutlet weak var scrollView: NSScrollView!
NSScrollView 的documentView 是NSTextView,因此在 Swift 中您可以使用以下内容:
let textView : NSTextView? = scrollView?.documentView as? NSTextView
textView?.string = text
【解决方案7】:
对于 NSScrollView:
@IBOutlet weak var textScroll: NSScrollView!
看看这个:
let text = textScroll.documentView!.textStorage!!
let attr = NSAttributedString(string: "Hello World")
text.appendAttributedString(attr)
这非常符合@mrtn.lxo 解决方案的精神,仅针对 NSScrollView 完成并且正在使用 NSAttributedString。