xcode 在 NSScrollView (Swift) 中显示文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25179008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Displaying text in a NSScrollView (Swift)
提问by ixany
Just started to learn Swift and created a little macOS app in which I want to use a NSScrollView
to display an attributed String. I've tried:
刚开始学习 Swift 并创建了一个小的 macOS 应用程序,我想在其中使用 aNSScrollView
来显示属性字符串。我试过了:
@IBOutlet var ScrollViewOutlet : NSScrollView
var attributedString = NSMutableAttributedString(string: myText)
ScrollViewOutlet.insertText(attributedString)
But that doesn't seem to work. Feels confusing since doing the exact same with a NSTextView
instead works like a charm.
但这似乎不起作用。感觉很混乱,因为用 a 做完全相同的事情,NSTextView
就像一个魅力。
What am I missing here?
我在这里错过了什么?
采纳答案by ixany
It seems that the earlier beta versions (which featured Swift) of Xcode had some serious issues with this kind of outlets (as you can see here: http://swiftwtf.tumblr.com/post/88387419568/nstextview). However, since Xcode 6 Beta 6
it works.
Xcode 的早期 beta 版本(以 Swift 为特色)似乎在这种插座上存在一些严重的问题(如您所见:http: //swiftwtf.tumblr.com/post/88387419568/nstextview)。然而,既然Xcode 6 Beta 6
它有效。
textFieldOutlet.textStorage.mutableString.setString("Hello w0rld!")
To handle the String, creating an outlet for the textField
instead of the scrollView
itself is also a better practice.
为了处理字符串,为textField
而不是scrollView
本身创建一个插座也是一种更好的做法。
回答by Hymansonsox
In the following example, I added the scrollView in the interface builder as my starting point.
在下面的示例中,我在界面构建器中添加了 scrollView 作为我的起点。
The following works if you your scrollView/textView is empty/blank OR if you need to append text in front of what is already in the scrollView/textView. If there is already text in the box, the new text is inserted in front of the existing text.
如果您的 scrollView/textView 为空/空白,或者如果您需要在 scrollView/textView 中已有的内容前面附加文本,则以下内容有效。如果框中已有文本,则将新文本插入到现有文本的前面。
The documentView is an NSTextView
documentView 是一个 NSTextView
Swift 4.0
斯威夫特 4.0
@IBOutlet weak var imageDestinationDirectory: NSScrollView!
...
let destinationText = "Text to display"
imageDestinationDirectory.documentView!.insertText(destinationText)
回答by Ken Thomases
Apple's has an article about setting up a text view inside a scroll view programmatically. Text System User Interface Layer Programming Guide: Putting an NSTextView Object in an NSScrollViewYou should read that for greatest understanding, but here's the code:
Apple 有一篇关于以编程方式在滚动视图中设置文本视图的文章。文本系统用户界面层编程指南:将 NSTextView 对象放入 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];
You can then set the text of the text view by operating on its textStorage
object:
然后您可以通过对其textStorage
对象进行操作来设置文本视图的文本:
theTextView.textStorage.attributedString = attributedString;
回答by madx
@IBOutlet weak var scrollView: NSScrollView!
The documentView
of the NSScrollView
is an NSTextView
, so in Swift you can use the following:
该documentView
的NSScrollView
是NSTextView
,这样的斯威夫特,你可以使用以下命令:
let textView : NSTextView? = scrollView?.documentView as? NSTextView
textView?.string = text
回答by Robert Waltera
answer of ixany worked great, but as a beginner it looks me more time to figure it out, that outlet should be done for NSTextView and not for NSTextScroll. NSTextView is hidden inside of NSTextScroll. All Results Field is NSTextScroll in my case and Text View Results is NSTextView
ixany 的答案效果很好,但作为初学者,我觉得我有更多时间来弄清楚,该出口应该为 NSTextView 而不是 NSTextScroll 完成。NSTextView 隐藏在 NSTextScroll 里面。 在我的情况下,所有结果字段都是 NSTextScroll,而文本视图结果是 NSTextView
回答by Alexander Borisenko
Here's how to do it programmatically, with auto layout:
以下是使用自动布局以编程方式执行此操作的方法:
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)
])
}
}
回答by Maxim Veksler
For NSScrollView:
对于 NSScrollView:
@IBOutlet weak var textScroll: NSScrollView!
Check this out:
看一下这个:
let text = textScroll.documentView!.textStorage!!
let attr = NSAttributedString(string: "Hello World")
text.appendAttributedString(attr)
This is very much in spirit of @mrtn.lxo solution, only done for NSScrollView and is using NSAttributedString.
这非常符合@mrtn.lxo 解决方案的精神,仅适用于 NSScrollView 并使用 NSAttributedString。