简体   繁体   English

如何在nstextview中设置文本的背景颜色?

[英]How do I set the background color of text in a nstextview?

I have a nstextview, and I was wondering how to set the background color of it. 我有一个nstextview,我想知道如何设置它的背景颜色。 I've tried quite a few things, but none seem to work. 我尝试过很多东西,但似乎都没有用。 They just set the actually text color. 他们只是设置实际的文本颜色。 I have a NSColorWell, called colorBox , and the textview is called doc , in case someone wants to know. 我有一个名为colorBox ,而textview被称为doc ,万一有人想知道。 I use `[self.colorBox color] to get the color in the NSColorWell 我使用`[self.colorBox color]来获取NSColorWell中的颜色

NSTextView has backgroundColor property . NSTextView具有backgroundColor属性 Try setting it to something else like NSColor.redColor() . 尝试将其设置为其他类似NSColor.redColor()

It also has drawsBackground property which need to be set to true to make it work. 它还有drawsBackground属性 ,需要将其设置为true才能使其工作。

let textView1 = NSTextView()
textView1.backgroundColor = NSColor.redColor()
textView1.drawsBackground = true

要更改属性字符串的背景颜色,必须将属性添加到其中:

[mutableAttributedString addAttribute:NSBackgroundColorAttributeName value:[UIColor redColor] range:selectedRange];

If you're using storyboards, TextViews have three sections in the Attributes Inspector, "Text View", "Scroll View", and "View". 如果您使用的是故事板,TextViews在属性检查器,“文本视图”,“滚动视图”和“视图”中有三个部分。 "Background color" is the fifth option in the "View" section. “背景颜色”是“视图”部分中的第五个选项。

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

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