NSFontManager Initialization

  • I am using NSFontManager and NSFontPanel to (eventually) extract a glyph
    as a draw object.  The following code fails unless the font panel has
    previously been used in a text field.

        NSFontManager* fmgr = [NSFontManager sharedFontManager];
        NSFont* theFont = [fmgr selectedFont];

        After I select the font and size in the panel, "theFont" will always
    return nil.  If I first use the panel with a text view, it will work
    properly.  Also, a few times, I couldn't set the size in the panel's size
    field.  Bug report time? Or is there something else I could do to get it
    properly initialized?
  • Gordon,

    -[NSFontManager selectedFont] is used mainly for getting the font set
    via -[NSFontManager setSelectedFont:isMultiple:] method.

    Typically, you query the user font selection via -convertFont: method.

    See the following tech doc section.
    Font Panel: Using the Font Panel

    Aki

    On 2008/03/02, at 14:29, Gordon Apple wrote:

    > I am using NSFontManager and NSFontPanel to (eventually) extract
    > a glyph
    > as a draw object.  The following code fails unless the font panel has
    > previously been used in a text field.
    >
    > NSFontManager* fmgr = [NSFontManager sharedFontManager];
    > NSFont* theFont = [fmgr selectedFont];
    >
    > After I select the font and size in the panel, "theFont" will
    > always
    > return nil.  If I first use the panel with a text view, it will work
    > properly.  Also, a few times, I couldn't set the size in the panel's
    > size
    > field.  Bug report time? Or is there something else I could do to
    > get it
    > properly initialized?
  • I guess I don't understand how to use that.  I have no font specified.
    ConvertFont returns a similar font style (e.g., Italic) and size to whatever
    you put into it.  What I want is to extract the user selected font and size
    from the panel.  "selectedFont" does what I want, as long as it has been
    used previously with text.  Here is what I am doing:

    -(void)addString:(NSString*)string toPath:(NSBezierPath*)path {
        NSFontManager* fmgr = [NSFontManager sharedFontManager];

    //    NSFont* theFont = [fmgr selectedFont];
        NSFont* theFont = [fmgr convertFont:[NSFont systemFontOfSize:50]];

        NSBezierPath* stringPath = [string bezierWithFont:theFont];
        NSAffineTransform* tf = [NSAffineTransform transform];
        [tf scaleXBy:1 yBy:-1];
        NSBezierPath* uppath = [tf transformBezierPath:stringPath];
        [path appendBezierPath:uppath];
    }

        BTW, the [string bezierWithFont:theFont] is straight out of the
    "SpeedyCategories" file in the SpeedometerView sample code.  Works great.
    I wish I had found this long ago.

    > Gordon,
    >
    > -[NSFontManager selectedFont] is used mainly for getting the font set
    > via -[NSFontManager setSelectedFont:isMultiple:] method.
    >
    > Typically, you query the user font selection via -convertFont: method.
    >
    > See the following tech doc section.
    > Font Panel: Using the Font Panel
    >
    > Aki
    >
    > On 2008/03/02, at 14:29, Gordon Apple wrote:
    >
    >> I am using NSFontManager and NSFontPanel to (eventually) extract
    >> a glyph
    >> as a draw object.  The following code fails unless the font panel has
    >> previously been used in a text field.
    >>
    >> NSFontManager* fmgr = [NSFontManager sharedFontManager];
    >> NSFont* theFont = [fmgr selectedFont];
    >>
    >> After I select the font and size in the panel, "theFont" will
    >> always
    >> return nil.  If I first use the panel with a text view, it will work
    >> properly.  Also, a few times, I couldn't set the size in the panel's
    >> size
    >> field.  Bug report time? Or is there something else I could do to
    >> get it
    >> properly initialized?
    >>
  • The standard idiom here is to implement -changeFont: method that
    receives action messages whenever user changes font selection.
    In the method, you can call -convertFont: to query the user selection.
    The result has to be kept in your object.
    http://developer.apple.com/documentation/Cocoa/Conceptual/FontPanel/Tasks/U
    singFontPanel.html#/

    /apple_ref/doc/uid/20000836

    Aki

    On 2008/03/03, at 13:12, Gordon Apple wrote:

    > I guess I don't understand how to use that.  I have no font
    > specified.
    > ConvertFont returns a similar font style (e.g., Italic) and size to
    > whatever
    > you put into it.  What I want is to extract the user selected font
    > and size
    > from the panel.  "selectedFont" does what I want, as long as it has
    > been
    > used previously with text.  Here is what I am doing:
    >
    > -(void)addString:(NSString*)string toPath:(NSBezierPath*)path {
    > NSFontManager* fmgr = [NSFontManager sharedFontManager];
    >
    > //    NSFont* theFont = [fmgr selectedFont];
    > NSFont* theFont = [fmgr convertFont:[NSFont systemFontOfSize:50]];
    >
    > NSBezierPath* stringPath = [string bezierWithFont:theFont];
    > NSAffineTransform* tf = [NSAffineTransform transform];
    > [tf scaleXBy:1 yBy:-1];
    > NSBezierPath* uppath = [tf transformBezierPath:stringPath];
    > [path appendBezierPath:uppath];
    > }
    >
    > BTW, the [string bezierWithFont:theFont] is straight out of the
    > "SpeedyCategories" file in the SpeedometerView sample code.  Works
    > great.
    > I wish I had found this long ago.
    >
    >> Gordon,
    >>
    >> -[NSFontManager selectedFont] is used mainly for getting the font set
    >> via -[NSFontManager setSelectedFont:isMultiple:] method.
    >>
    >> Typically, you query the user font selection via -convertFont:
    >> method.
    >>
    >> See the following tech doc section.
    >> Font Panel: Using the Font Panel
    >>
    >> Aki
    >>
    >> On 2008/03/02, at 14:29, Gordon Apple wrote:
    >>
    >>> I am using NSFontManager and NSFontPanel to (eventually) extract
    >>> a glyph
    >>> as a draw object.  The following code fails unless the font panel
    >>> has
    >>> previously been used in a text field.
    >>>
    >>> NSFontManager* fmgr = [NSFontManager sharedFontManager];
    >>> NSFont* theFont = [fmgr selectedFont];
    >>>
    >>> After I select the font and size in the panel, "theFont" will
    >>> always
    >>> return nil.  If I first use the panel with a text view, it will work
    >>> properly.  Also, a few times, I couldn't set the size in the panel's
    >>> size
    >>> field.  Bug report time? Or is there something else I could do to
    >>> get it
    >>> properly initialized?
    >>>
    >
  • Therein lies the problem.  When the user clicks the text tool and clicks
    a position on the view, I pop a sheet to enter the string and where the the
    font panel can be launched to set font and size.  When the sheet is closed,
    the Bezier is created and the string appears as a Bezier shape object.
    There is no font to convert.  I just want the font and size that the user
    selected in the panel.

        Side comment:  The machinations that one has to go through to get the
    Bezier of the text is nothing short of ridiculous.  If it weren't for the
    Speedometer code, I would have never figured it out.  There needs to be a
    better way to get an array of glyphs than the
    subclass/method-parameter-intercept techniques in Speedometer.  I'm going to
    put in an enhancement request.

    > The standard idiom here is to implement -changeFont: method that
    > receives action messages whenever user changes font selection.
    > In the method, you can call -convertFont: to query the user selection.
    > The result has to be kept in your object.
    > http://developer.apple.com/documentation/Cocoa/Conceptual/FontPanel/Tasks/U
    sin

    > gFontPanel.html#/
    > /apple_ref/doc/uid/20000836
    >
    > Aki
previous month march 2008 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today