NSTextField(标签)属性文本:选择
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了同样的问题。不幸的是,接受的答案并没有解决它。即使我设置了
attributedStringValue
,我的文本样式在被选择时仍然发生变化。感谢在此问题中的回复,我能够通过调用以下方法部分解决问题:
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:
好的。经过进一步研究为什么白色会“粘住”而绿色不会“粘住”,我发现在 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.