如何检索只有风格的字体(大胆,斜体、粗斜体)从字面板(NSFontPanel)和颜色?

StackOverflow https://stackoverflow.com/questions/3606329

我检索的一个值 NSFontPanel, ,这改变了字体的家庭、风格的文本。但我想只有风格的字体和颜色分开,我已选择从 NSFontPanel.

我没有得到如何获得的。

NSFont *font  =[fontmanager selectedFont];
     string =[font fontName];
     st = [font pointSize];
    color =[TextEntered textColor];
  1. 在串变我得到字体的家庭(e。g。宋体)和风格的字体(例如大胆).但我希望这些价值观分开。

  2. 和颜色变我只是得到黑色的颜色。

我不能明白我在哪里出错得到的颜色,什么功能我用得到字体的风格。。

有帮助吗?

解决方案

得到的风格,则应使用 [fontmanager traitsOfFont: font].以色,我觉得你想要得到文本作为一个 NSAttributedString 然后,得到的价值的属性 NSForegroundColorAttributeName.

编辑要添加:假设你TextEntered变量NSTextField*,使用这样的事情:

NSAttributedString* theText = [TextEntered attributedStringValue];
NSColor* theColor = (NSColor*)[theText attribute: NSForegroundColorAttributeName
    atIndex: 0 effectiveRange: NULL ];

对于删除线,使用 NSStrikethroughStyleAttributeName 并强调使用 NSUnderlineStyleAttributeName.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top