如何格式化/着色 NSTextView 字符串的技术

发布于 2024-12-28 20:42:02 字数 967 浏览 1 评论 0 原文

我正在寻找一种可靠的技术来在 NSTextView 中进行简单的字符串格式化(粗体,斜体,...)。文本解析几乎是用正则表达式完成的,但现在我需要应用字体特征并更改大小。

关于如何将文本加粗的一些代码片段

[[textView textStorage] beginEditing];
[[textView textStorage] applyFontTraits:NSFontBoldTrait range:range];
[[textView textStorage] endEditing];

以及大小的变化都可以

[[textView textStorage] beginEditing];  
NSFont* font = [[textView textStorage] attribute:NSFontAttributeName atIndex:range.location effectiveRange:nil];

NSFont* newFont = [NSFont fontWithName:[font familyName] 
                                  size:[font pointSize] + size];

[[textView textStorage] addAttribute:NSFontAttributeName 
                               value:newFont 
                               range:range];
[[textView textStorage] endEditing];

正常工作。我现在遇到的唯一问题是,在某些情况下,当我键入新字符时,这些字符默认为粗体或斜体,即使我没有将属性应用于它们。

我是否必须使用 NSTextViewsetTypingAttributes 重置某些内容,或者我只是错过了这里的某些内容?

I'm looking for a reliable technique to do simple string formatting(bold, italic,...) in a NSTextView. The text parsing is almost done with regex, but now I need to apply font trait and also change the size.

Some code snippets on how I make a text bold

[[textView textStorage] beginEditing];
[[textView textStorage] applyFontTraits:NSFontBoldTrait range:range];
[[textView textStorage] endEditing];

This and also the size changes with

[[textView textStorage] beginEditing];  
NSFont* font = [[textView textStorage] attribute:NSFontAttributeName atIndex:range.location effectiveRange:nil];

NSFont* newFont = [NSFont fontWithName:[font familyName] 
                                  size:[font pointSize] + size];

[[textView textStorage] addAttribute:NSFontAttributeName 
                               value:newFont 
                               range:range];
[[textView textStorage] endEditing];

works fine. The only problem I have now is that in some cases, when I type new characters, those characters are bold or italic by default, even though I don't apply the properties to them.

Do I have to reset something with the setTypingAttributes of the NSTextView or do I simply miss something here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゞ记忆︶ㄣ 2025-01-04 20:42:02

我认为您设置 typingAttributes 的方法是正确的。 -setTypingAttributes 的参考:

但是,如果您添加任何更改文本属性的用户操作,则该操作应使用此方法来应用这些属性。更改属性的用户操作应始终设置键入属性,因为在下次键入之前可能不会发生后续的选择更改。

这似乎适用于你的情况。

我不知道所描述的行为是否仅适用于 TextEdit 等所见即所得编辑器。您似乎正在处理一些行为与具有语法突出显示的编辑器类似的东西。在那里,您永远不想手动更改文本属性,而是更改语法的结构。它可能不适合这种情况,您应该重置 typingAttributes 或根据解析进行设置。

I think you are right with the approach to set typingAttributes. Reference for -setTypingAttributes: says

However, if you add any user actions that change text attributes, the action should use this method to apply those attributes afterwards. User actions that change attributes should always set the typing attributes because there might not be a subsequent change in selection before the next typing.

That seems to apply in your case.

I don't know if the described behaviour is only correct for WYSIWYG editors like TextEdit. You seem to work on something that is similar in behaviour to an editor with syntax highlighting. There you never really want to change text attributes manually, but rather on structure from a grammar. It probably doesn't fit in that case, and you should reset typingAttributes or set it according to parsing up to there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文