NSTextField(标签)属性文本:选择

发布于 2024-09-03 08:29:13 字数 222 浏览 13 评论 0原文

我有一个 NSTextField 标签,其中包含属性文本。颜色和尺寸与默认值不同。 (文本稍大且呈绿色)现在,我需要能够选择标签中的文本,以便单击嵌入在属性文本中的超链接。这很好用;但是,当选择文本时,文本格式将恢复为默认值(较小的黑色字体)。

是否有办法阻止这种行为,以便当用户单击(选择文本)标签时保留我的样式?

编辑:根据记录,我有另一个带有白色文本的标签,选择时不会改变颜色,只会改变大小。

I have a NSTextField Label that has attributed text in it. The color and size are different from the defaults. (The text is a bit larger and green) Now, I need to be able to select the text in the label so as the click the hyperlinks embedded in the attributed text. This works fine; however, when selecting the text the format of the text reverts back to the defaults (a smaller, black font).

Is there anyway to stop this behavior so that my styling is preserved when the user clicks (selects text) on a Label?

EDIT: For the record, I have another label with white text that does not change color when selected, only size.

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

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

发布评论

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

评论(2

萌辣 2024-09-10 08:29:13

我遇到了同样的问题。不幸的是,接受的答案并没有解决它。即使我设置了 attributedStringValue,我的文本样式在被选择时仍然发生变化。

感谢在此问题中的回复,我能够通过调用以下方法部分解决问题:

label.allowsEditingTextAttributes = true

I ran into the same issue. The accepted answer didn't fix it unfortunately. My text style was still changing when being selected even though I had set the attributedStringValue.

Thanks to the response in this question, I was able to in part solve the problem by calling:

label.allowsEditingTextAttributes = true

柠檬色的秋千 2024-09-10 08:29:13

好的。经过进一步研究为什么白色会“粘住”而绿色不会“粘住”,我发现在 Interface Builder 中设置的任何文本属性在选择后都不会保留。

因此,解决方案是始终使用 NSAttributedString 调用 setAttributedStringValue:,该 NSAttributedString 具有您希望确保在选择标签期间保留的属性。

Okay. After further research as to why the white color was 'sticking' and why the green was not, I discovered that any text attributes set in Interface Builder do not stay after selection.

Therefore, the solution is to always call setAttributedStringValue: with an NSAttributedString that has attributes that you want to make sure stay during selection on your label.

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