如何使用显式NSLayoutManager,NSTextStorage,NSTextContainer以编程方式设置NSTextView? [英] How to set up an NSTextView programmatically with explicit NSLayoutManager, NSTextStorage, NSTextContainer?

查看:115
本文介绍了如何使用显式NSLayoutManager,NSTextStorage,NSTextContainer以编程方式设置NSTextView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在苹果文档之后,我试图通过其两个构造函数方法设置一个简单的 NSTextView

Following the apple documentation I am trying to set up a simple NSTextView via its two constructor methods.

我将以下代码放入内容视图的视图控制器的 viewDidAppear 方法中。 textView是 NSTextView 的实例,frameRect是内容视图的框架。

I am placing the below code inside the viewDidAppear method of the view controller of the content view. textView is an instance of NSTextView, frameRect is the frame of the content view.

以下Swift代码可以正常工作(给我一个可编辑的textView并在屏幕上显示文本):

The following Swift code works (gives me an editable textView with the text showing on the screen):

    textView = NSTextView(frame: frameRect!)
    self.view.addSubview(textView)
    textView.textStorage?.appendAttributedString(NSAttributedString(string: "Hello"))

以下操作无效(文本视图不可编辑,并且屏幕上未显示文本):

The following does NOT work (text view is not editable and no text shown on the screen):

    var textStorage = NSTextStorage()
    var layoutManager = NSLayoutManager()
    textStorage.addLayoutManager(layoutManager)
    var textContainer = NSTextContainer(containerSize: frameRect!.size)
    layoutManager.addTextContainer(textContainer)
    textView = NSTextView(frame: frameRect!, textContainer: textContainer)

    textView.editable = true
    textView.selectable = true
    self.view.addSubview(textView)

    textView.textStorage?.appendAttributedString(NSAttributedString(string: "Hello more complex"))

第二个示例中我在做什么错?我试图遵循苹果《可可文本体系结构指南》中给出的示例,他们讨论通过显式实例化其辅助对象网络来设置 NSTextView

What am I doing wrong in the second example? I am trying to follow the example given in Apple's "Cocoa Text Architecture Guide" where they discuss setting up an NSTextView by explicitly instantiating its web of helper objects.

推荐答案

您需要保留对创建的 NSTextStorage 变量的引用。我不太确定这方面的机制,但看起来文本视图仅对其文本存储对象保持弱引用。该对象超出范围后,将无法再用于文本视图。我想这与MVC设计模式保持一致,在该模式下,视图(在本例中为 NSTextView )应独立于其模型(在 NSTextStorage 对象)。

You need to keep a reference to the NSTextStorage variable you create. I'm not quite sure about the mechanics of it all, but it looks like the text view only keeps a weak reference to its text storage object. Once this object goes out of scope, it's no longer available to the text view. I guess this is in keeping with the MVC design pattern, where views (the NSTextView in this case) are meant to be independent of their models (the NSTextStorage object).

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet weak var window: NSWindow!

    var textView: NSTextView!
    var textStorage: NSTextStorage! // STORE A REFERENCE

    func applicationDidFinishLaunching(aNotification: NSNotification) {
        var view = window.contentView as NSView
        textStorage = NSTextStorage()
        var layoutManager = NSLayoutManager()
        textStorage.addLayoutManager(layoutManager)
        var textContainer = NSTextContainer(containerSize: view.bounds.size)
        layoutManager.addTextContainer(textContainer)
        textView = NSTextView(frame: view.bounds, textContainer: textContainer)

        textView.editable = true
        textView.selectable = true
        view.addSubview(textView)

        textView.textStorage?.appendAttributedString(NSAttributedString(string: "Hello more complex"))
    }
}

这篇关于如何使用显式NSLayoutManager,NSTextStorage,NSTextContainer以编程方式设置NSTextView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆