【问题标题】:How to retrieve only font style(Bold,italic,Bold Italic) from font panel(NSFontPanel) and color?如何从字体面板(NSFontPanel)和颜色中仅检索字体样式(粗体,斜体,粗斜体)?
【发布时间】:2010-08-31 05:48:29
【问题描述】:

我正在从NSFontPanel 中检索一个值,它会更改字体系列和文本样式。但我只想要从NSFontPanel 中选择的字体样式和颜色。

我不知道如何获得它。

NSFont *font  =[fontmanager selectedFont];
     string =[font fontName];
     st = [font pointSize];
    color =[TextEntered textColor];
  1. 在字符串变量中,我得到字体系列(例如 Arial)和字体样式(例如粗体)。但我想要这些值分开。

  2. 而在颜色变量中,我只能得到黑色。

我不明白我在获取颜色方面哪里错了,以及我用什么函数来获取字体样式..

【问题讨论】:

    标签: objective-c cocoa nsfont nsfontpanel


    【解决方案1】:

    要获得样式,您可以使用[fontmanager traitsOfFont: font]。至于颜色,我想你想将文本作为NSAttributedString 获取,然后获取属性NSForegroundColorAttributeName 的值。

    编辑添加:假设您的 TextEntered 变量是 NSTextField*,请使用如下内容:

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

    删除线使用NSStrikethroughStyleAttributeName,下划线使用NSUnderlineStyleAttributeName

    【讨论】:

    • 获取颜色的示例。并感谢您的回答..它适用于获取字体样式。
    • 请您告诉我如何从 NSFontPanel 中划线和下划线。
    猜你喜欢
    • 2011-07-18
    • 2012-12-10
    • 2019-07-20
    • 1970-01-01
    • 2016-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-07
    相关资源
    最近更新 更多