如何使用显式NSLayoutManager,NSTextStorage,NSTextContainer以编程方式设置NSTextView?

时间:2015-03-13 14:55:46

标签: cocoa swift nstextview nslayoutmanager nstextstorage

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

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

以下Swift代码有效(给我一个可编辑的textView,屏幕上显示文字):

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

以下内容不起作用(文本视图不可编辑且屏幕上未显示任何文字):

    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"))

我在第二个例子中做错了什么?我试图按照Apple的“Cocoa Text Architecture Guide”中给出的示例,他们通过显式实例化其辅助对象Web来讨论设置NSTextView

3 个答案:

答案 0 :(得分:6)

您需要保留对您创建的NSTextStorage变量的引用。我不太确定这一切的机制,但看起来文本视图只保留对其文本存储对象的弱引用。一旦该对象超出范围,它就不再可用于文本视图。我想这与MVC设计模式保持一致,其中视图(在这种情况下为NSTextView)意味着独立于其模型(NSTextStorage对象)。

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"))
    }
}

答案 1 :(得分:0)

#import <Cocoa/Cocoa.h>

@interface TextViewController : NSObject {

    NSLayoutManager *secondLayout;

    IBOutlet NSSplitView *columnView;
    IBOutlet NSTextView *bottomView;

}

- (IBAction) addColumn: (id)sender;

@end
#import "TextViewController.h"

@implementation TextViewController

- (void)awakeFromNib
{
    NSTextStorage *storage = [bottomView textStorage];
    secondLayout = [NSLayoutManager new];
    [storage addLayoutManager: secondLayout];
    [secondLayout release];
    [self addColumn: nil];
    [self addColumn: nil];
}


- (IBAction) addColumn: (id)sender
{
    NSRect frame = [columnView frame];

    NSTextContainer *container = [[NSTextContainer alloc]
                                  initWithContainerSize: frame.size];
    [container setHeightTracksTextView: YES];
    [container setWidthTracksTextView: YES];

    [secondLayout addTextContainer: container];
    [container release];
    NSTextView *newView = [[NSTextView alloc] initWithFrame: frame
                                              textContainer: container];
    [columnView addSubview: newView];
    [newView release];
}

@end

答案 2 :(得分:0)

在 Xcode 12.4 下测试。在游乐场:

import Cocoa
import AppKit

let textViewFrame = CGRect(x: 0, y: 0, width: 250, height: 90)
let textStorage = NSTextStorage()
var layoutManager = NSLayoutManager()
textStorage.addLayoutManager(layoutManager)
var textContainer = NSTextContainer(containerSize: textViewFrame.size)
layoutManager.addTextContainer(textContainer)
let textView = NSTextView(frame: textViewFrame, textContainer: textContainer)
textView.isEditable = true
textView.isSelectable = true
textView.textColor = NSColor.red
textView.string = "Why is this so complicated..."