从界面生成器可编辑的nstextview [英] editable nstextview from interface builder

查看:157
本文介绍了从界面生成器可编辑的nstextview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法编辑nstextview. 这是我尝试的步骤.

I'm unable get to edit an nstextview. Here is the steps I tried.

  1. 从界面构建器中,我将自定义视图"拖放到了另一个视图中. (我在那里找不到nstextview.)
  2. 我将自定义视图表单"NSView"的类更改为"NSTextView"
  3. 接下来我运行我的项目,我可以看到文本视图正在渲染(鼠标光标在文本视图区域的鼠标悬停上已更改为文本模式)
  4. 但是,我无法插入/键入/编辑任何文本.

注意:我已经尝试过setEditable,setSelectable&设置其他帖子中推荐的firstResponder选项,但没有帮助.

Note: I have tried setEditable, setSelectable & set firstResponder options recommended in other posts, which did not help.

推荐答案

问题在于,当您以上述方式使用Interface Builder时,无法在NSTextView上正确设置textStorage.

The problem is that the textStorage is not set up correctly on the NSTextView when you use Interface Builder in the way described.

我也想在InterfaceBuilder中使用不带scrollView的NSTextView.在Xcode 10中,似乎不可能在单独的自定义视图层次结构中放置一个单独的NSTextView(早期答案暗示这是可能的: https ://stackoverflow.com/a/2398980/978300 ).

I too wanted to use an NSTextView, without scrollView, in InterfaceBuilder. In Xcode 10 it doesn't seem possible to put a lone NSTextView in some custom view hierarchy (earlier answers have hinted that this was possible: https://stackoverflow.com/a/2398980/978300).

可以通过问题中的"CustomView"方法使用此方法-但是,这将仅在属性"检查器中具有简单的NSView属性(即,您将无法自定义字体等).您可以通过在自定义类上使用@IBInspectable传递一些详细信息.

It is possible to get this working with the "CustomView" method in the question - however, this will only have the simple NSView properties in the Attributes inspector (i.e. you won't be able to customise font etc). You could pass some details through using @IBInspectable on your custom class.

连接检查器似乎正常工作.

The Connections Inspector seems to work correctly.

示例NSTextView子类...

Example NSTextView subclass...

class MyTextView: NSTextView
{
    // init(frame: sets up a default textStorage we want to mimic in init(coder:
    init() {
        super.init(frame: NSRect.zero)
        configure()
    }

    /*
     It is not possible to set up a lone NSTextView in Interface Builder, however you can set it up
     as a CustomView if you are happy to have all your presentation properties initialised
     programatically. This initialises an NSTextView as it would be with the default init...
     */
    required init(coder: NSCoder) {
        super.init(coder: coder)!

        let textStorage = NSTextStorage()
        let layoutManager = NSLayoutManager()
        textStorage.addLayoutManager(layoutManager)

        // By default, NSTextContainers do not track the bounds of the NSTextview
        let textContainer = NSTextContainer(containerSize: CGSize.zero)
        textContainer.widthTracksTextView = true
        textContainer.heightTracksTextView = true

        layoutManager.addTextContainer(textContainer)
        replaceTextContainer(textContainer)

        configure()
    }

    private func configure()
    {
        // Customise your text here...
    }
}

这篇关于从界面生成器可编辑的nstextview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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