[英]Cocoa NSTextField - binding and setting attributes
I can change the NSString in my controller class and I see it reflected in the label. 我可以在控制器类中更改NSString,然后将其反映在标签中。
I can change the NSFont in my controller class and I see that reflected in the label. 我可以在我的控制器类中更改NSFont,我看到它反映在标签中。
But... 但...
I can't for the life of me figure out how to turn on and off underlining. 我一辈子都无法弄清楚如何打开和关闭下划线。
If I call this function... 如果我调用此函数...
-(void)setUnderlineType:(NSNumber*)underline
{
NSMutableAttributedString* content = [[TheLabel attributedStringValue] mutableCopy];
[content addAttribute:NSUnderlineStyleAttributeName value:underline range:NSMakeRange(0, content.length)];
[TheLabel setAttributedStringValue:content];
}
... I get an underline, but then the bound font is ignored and I get some standard font. ...我得到一个下划线,但是然后绑定字体被忽略,我得到一些标准字体。 From then on, changing the NSFont in my controller has no visible effect on the NSTextField.
从那时起,更改我的控制器中的NSFont对NSTextField没有可见的影响。
I tried removing attributes from 'content' before adding the underline... removing the font attributes... but that doesn't work either. 我尝试在添加下划线之前从“内容”中删除属性...删除字体属性...但这也不起作用。
Any time I call this function, the font that is bound to the NSTextField become 'ignored' and I see a standard font is a standard size. 每当我调用此函数时,绑定到NSTextField的字体都会被“忽略”,并且我看到标准字体是标准大小。
Any guidance would be greatly appreciated. 任何指导将不胜感激。
You need to set NSFontAttributeName
to update a font of NSAttributedString
. 您需要设置
NSFontAttributeName
更新的字体NSAttributedString
。
NSFont *font = ...;
NSMutableAttributedString* content = [[theLabel attributedStringValue] mutableCopy];
[content addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, content.length)];
[theLabel setAttributedStringValue:content];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.