在 NSScrollView (Swift) 中显示文本 [英] Displaying text in a NSScrollView (Swift)
问题描述
刚开始学习 Swift 并创建了一个小的 macOS 应用程序,我想在其中使用 NSScrollView
来显示属性字符串.我试过了:
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:
@IBOutlet var ScrollViewOutlet : NSScrollView
var attributedString = NSMutableAttributedString(string: myText)
ScrollViewOutlet.insertText(attributedString)
但这似乎行不通.感觉很混乱,因为对 NSTextView
做完全相同的事情,而不是像魅力一样工作.
But that doesn’t seem to work. Feels confusing since doing the exact same with a NSTextView
instead works like a charm.
我在这里遗漏了什么?
推荐答案
Xcode 的早期 beta 版本(以 Swift 为特色)似乎在这种类型的插座上存在一些严重的问题(如您所见:http://swiftwtf.tumblr.com/post/88387419568/nstextview).但是,由于 Xcode 6 Beta 6
它可以工作.
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.
textFieldOutlet.textStorage.mutableString.setString("Hello w0rld!")
为了处理字符串,为 textField
创建一个 outlet 而不是 scrollView
本身也是一个更好的做法.
To handle the String, creating an outlet for the textField
instead of the scrollView
itself is also a better practice.
这篇关于在 NSScrollView (Swift) 中显示文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!