【问题标题】:Paragraph Style Not Updating on NSTextView with Value Binding带有值绑定的 NSTextView 上的段落样式未更新
【发布时间】:2018-11-05 15:47:53
【问题描述】:

我有一个绑定到字符串的 NSTextView。 NSTextView 将 isRichText 设置为 false。 文本可由用户编辑。

我想动态更新字体和行高。 使用 textView.font 设置字体随时可用。

我似乎只能设置段落样式,用于绑定之前的行高。之后,相同的代码对文本布局没有影响。

我正在使用以下代码:

let paragraphStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
paragraphStyle.lineHeightMultiple = style.lineHeightMultiple
editableText.defaultParagraphStyle = paragraphStyle

谁能帮我解决我做错了什么? 谢谢。

【问题讨论】:

  • defaultParagraphStyle 在文本视图中没有文本并且用户开始输入时使用。更改文本视图中文本的段落样式或绑定中使用的属性字符串。
  • 谢谢@Willeke。我没有使用属性字符串,而是绑定到一个简单的字符串。我还需要通过 textStorage 更新内容吗?

标签: cocoa nstextview


【解决方案1】:

感谢@Willeke 和this question,我有一个解决方案。正如建议的那样,我不得不为此转到文本存储,并在整个文本范围内设置属性。不过,这确实感觉不对,因为我将 NSTextView 设置为纯文本,因此我认为可以批量应用样式。

如果设置属性如下,则 textView.font 设置被删除,因此所有属性必须以相同的方式应用。

let paragraphStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
paragraphStyle.lineHeightMultiple = style.lineHeightMultiple

let storageRange = NSMakeRange(0, editableText.textStorage!.string.count)
editableText.textStorage!.setAttributes(
            [NSAttributedStringKey.paragraphStyle : paragraphStyle,
             NSAttributedStringKey.font : style.noteFont],
            range: storageRange)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-08
    • 2022-01-18
    • 2019-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多