NSTextView颜色和字体清除后重置 [英] NSTextView color and font reset after clearing it
问题描述
但是,当我用 setString:@清除视图时,
,不仅文本消失,而且所有默认颜色/字体格式化。
> setString ,文本会变成默认字体并再次变黑。
有人可以解释为什么会是这样,
UPDATED:
感谢您帮助和澄清问题。
我最后只是在我setString后格式化它。
[self.txtLog setFont:[NSFont fontWithName:@ couriersize:12]];
[self.txtLog setTextColor:[NSColor colorWithSRGBRed:65.0 / 255 green:229.0 / 255 blue:235.0 / 255 alpha:1]];
这是因为清除NSTextView并设置一个非属性)字符串删除格式化,其中包括字体和颜色。
大多数时候,我设置了使用HTML格式化的属性字符串。我觉得很方便。例如,这里有两个宏我用来设置颜色和字体:
#define color(string,color)strAdd5 (@< font color = \#,(color),@\>,(string),@< / font>)
#define fontName fontname)strAdd5(@< span style = \font-family:,(fontname),@; \>,(string),@< / span>)
#define html2AttributedString(htmlString)[[[NSAttributedString alloc] initWithHTML:[(htmlString)dataUsingEncoding:NSUTF8StringEncoding] documentAttributes:NULL] autorelease]
然后,我需要的是在文本存储中设置属性字符串:
[textStorage setAttributedString:html2AttributedString(color(myNSString,@FF0000))]
[textView insertText:html2AttributedString(fontName(myNSString,@Courier New))
使用HTML,您可以定义一切,包括定中心和许多其他属性。但是,你必须将任何新行转换为< BR />,然后再转换文字。
I set some default font and color to a NSTextView inside of a NSPanel.
However, when I clear the view with setString:@""
, not only does the text disappears, but all the default colors/fonts formatting.
After I do another setString
, the text becomes the default font and black again.
Can someone explain why that is and what can I do to remedy it?
UPDATED:
Thanks for the help and clarification of the issue. I ended up just formatting it again after I setString.
[self.txtLog setFont:[NSFont fontWithName:@"courier" size:12]];
[self.txtLog setTextColor:[NSColor colorWithSRGBRed:65.0/255 green:229.0/255 blue:235.0/255 alpha:1]];
It is because clearing the NSTextView and setting a regular (non-attributed) string removes the formatting, which includes font and color.
Most of the time, I set attributed strings that I format using HTML. I find it to be very convenient. For instance, here are two macro I use to set the color and the font:
#define color(string, color) strAdd5(@"<font color=\"#",(color), @"\">", (string), @"</font>")
#define fontName(string, fontname) strAdd5(@"<span style=\"font-family: ",(fontname), @";\">", (string), @"</span>")
#define html2AttributedString(htmlString) [[[NSAttributedString alloc] initWithHTML:[(htmlString) dataUsingEncoding:NSUTF8StringEncoding]documentAttributes:NULL] autorelease]
Then, all I need is to set the attributed string in the text storage:
[textStorage setAttributedString:html2AttributedString(color(myNSString, @"FF0000"))]
or to use insertText of NSTextView
[textView insertText:html2AttributedString(fontName(myNSString, @"Courier New"))];
And using HTML, you can define everything including centering, and many other properties. But, you have to convert any new lines into < BR / > before converting the text.
这篇关于NSTextView颜色和字体清除后重置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!