简体   繁体   English

NSTextView 不会从 NSFontPanel 更改字体大小或颜色

[英]NSTextView not changing font size or colour from NSFontPanel

I created a basic NSTextView, I selected the following options in Interface Builder:我创建了一个基本的 NSTextView,我在 Interface Builder 中选择了以下选项:

  • Editable可编辑
  • Selectable可选择
  • Field Editor字段编辑器
  • Rich Text富文本
  • Undo撤消
  • Graphics图形
  • Non-contiguous Layout非连续布局
  • Font Panel字体面板
  • Ruler统治者
  • Inspector Bar检查员栏

I set the NSViewController to be the delegate of the NSTextView and the only other custom thing I've done for this NSTextView is to enable inserting tabs and new lines (by accepting First responder):我将 NSViewController 设置为 NSTextView 的委托,并且我为此 NSTextView 所做的唯一其他自定义操作是启用插入选项卡和新行(通过接受第一响应者):

    func textView(_ textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {

    if commandSelector == #selector(insertNewline(_:)) {
        textView.insertNewlineIgnoringFieldEditor(self)
        return true
    } else if commandSelector == #selector(insertTab(_:)) {
        textView.insertTabIgnoringFieldEditor(self)
        return true
    } //else if commandSelector == #selector(changeColor(_:)) {
        //textView.setTextColor(NSFontPanel.colo, range: <#T##NSRange#>)
    //}

    return false

}

When I try to use the commands from the Font Panel + Inspector Bar, All the commands work fine except changing Font size or colour, is there anything that could be wrong?当我尝试使用 Font Panel + Inspector Bar 中的命令时,除了更改字体大小或颜色之外,所有命令都可以正常工作,是否有任何错误? Or do I need to do extra binding/delegates, etc for this to work?或者我是否需要做额外的绑定/委托等才能使其工作?

It is strange because if I change the Font itself (of a selected text) or the weight, it works fine (no coding was needed).这很奇怪,因为如果我更改字体本身(选定文本的)或粗细,它可以正常工作(不需要编码)。

Update更新

I've found the root of the problem causing this.我已经找到了导致这个问题的根源。 I'm displaying the TextView in a ViewController that is displayed using a Modal segue.我在使用 Modal segue 显示的 ViewController 中显示 TextView。 If I change from Modal to Show, the size and colour work fine.如果我从 Modal 更改为 Show,则大小和颜色都可以正常工作。 There's also no need for the extra commands for insert new line and tab.也不需要用于插入新行和制表符的额外命令。

Is there any reason why this is the case?有什么原因造成这种情况吗? Is there any customisation that should be done to the segue to avoid this?是否应该对 segue 进行任何自定义以避免这种情况? And, why is the view controller presentation affecting the behaviour of the font panel?而且,为什么视图控制器呈现会影响字体面板的行为?

NSFontPanel has a 'worksWhenModal' property which sounds as if it might be set to 'false'. NSFontPanel 有一个“worksWhenModal”属性,听起来好像它可能被设置为“false”。

A Boolean that indicates whether the receiver allows fonts to be changed in modal windows and panels.一个布尔值,指示接收器是否允许在模式窗口和面板中更改字体。Documentation:文档:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM