Check font is installed

  • Hello,

    I'm probably missing the obvious here, but searches on
    "installed" and "fonts" only bring up information
    about putting together a menu of installed fonts, and
    adding "check" to ths search only returns very little
    indeed. :)

    My program sets up certain default fonts for its
    various editors and views in its Preferences. If any
    of these fonts are not installed on the user's
    machine, however, this can cause my program to act in
    very weird ways - it completely messes up the
    interface and renders it unusable until the user
    alters the Preferences so that the fonts are set to
    something that is actually on the system and then
    restarts.

    What is the best way to check whether a given font is
    on the system and return a different font if not?
    Would something like this be enough:

    NSString *defaultFontName = [[[NSFontManager
    sharedFontManager] availableFonts]
    containsObject:@"Optima"] ? @"Optima" : @"Helvetica";

    Even then, what if the user has deleted "Helvetica"?
    Is this safe or is there a better way?

    Thanks in advance,
    Keith

    __________________________________________________
    Do You Yahoo!?
    Tired of spam?  Yahoo! Mail has the best spam protection around
    http://mail.yahoo.com
  • Hi Keith

    I believe you can use [NSFont fontWithName:size:] and if that method
    returns nil, you'll know you are requesting an uninstalled font--or
    your font name isn't right. =)

    Have a look at the NSFont docs for more details.

    -Phil

    On Oct 2, 2006, at 8:46 PM, Keith Blount wrote:

    > Hello,
    >
    > I'm probably missing the obvious here, but searches on
    > "installed" and "fonts" only bring up information
    > about putting together a menu of installed fonts, and
    > adding "check" to ths search only returns very little
    > indeed. :)
    >
    > My program sets up certain default fonts for its
    > various editors and views in its Preferences. If any
    > of these fonts are not installed on the user's
    > machine, however, this can cause my program to act in
    > very weird ways - it completely messes up the
    > interface and renders it unusable until the user
    > alters the Preferences so that the fonts are set to
    > something that is actually on the system and then
    > restarts.
    >
    > What is the best way to check whether a given font is
    > on the system and return a different font if not?
    > Would something like this be enough:
    >
    > NSString *defaultFontName = [[[NSFontManager
    > sharedFontManager] availableFonts]
    > containsObject:@"Optima"] ? @"Optima" : @"Helvetica";
    >
    > Even then, what if the user has deleted "Helvetica"?
    > Is this safe or is there a better way?
    >
    > Thanks in advance,
    > Keith
    >
    > __________________________________________________
    > Do You Yahoo!?
    > Tired of spam?  Yahoo! Mail has the best spam protection around
    > http://mail.yahoo.com
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/<dev...>
    >
    > This email sent to <dev...>
    >
  • On 02 Oct 06, at 11:46, Keith Blount wrote:
    > Even then, what if the user has deleted "Helvetica"?
    > Is this safe or is there a better way?

    Helvetica is a system font, so it should always be available. If the
    user has deleted it, then any misbehavior is their fault, not yours.
  • Thanks for your answers - much appreciated. Something
    as simple as trying to create the font and checking to
    see if it is nil in my preferences code didn't even
    occur to me - d'oh!

    So, wherever a default font can be called, I am now
    just checking whether it is nil or not and setting the
    font to -systemFontOfSize:whatever if so. Hopefully
    this should resolve things - I'll keep my fingers
    crossed for no more bug reports about this one. :)

    Thanks again and all the best,
    Keith

    --- Philip Dow <dev...> wrote:

    > Hi Keith
    >
    > I believe you can use [NSFont fontWithName:size:]
    > and if that method
    > returns nil, you'll know you are requesting an
    > uninstalled font--or
    > your font name isn't right. =)
    >
    > Have a look at the NSFont docs for more details.
    >
    > -Phil
    >
    > On Oct 2, 2006, at 8:46 PM, Keith Blount wrote:
    >
    >> Hello,
    >>
    >> I'm probably missing the obvious here, but
    > searches on
    >> "installed" and "fonts" only bring up information
    >> about putting together a menu of installed fonts,
    > and
    >> adding "check" to ths search only returns very
    > little
    >> indeed. :)
    >>
    >> My program sets up certain default fonts for its
    >> various editors and views in its Preferences. If
    > any
    >> of these fonts are not installed on the user's
    >> machine, however, this can cause my program to act
    > in
    >> very weird ways - it completely messes up the
    >> interface and renders it unusable until the user
    >> alters the Preferences so that the fonts are set
    > to
    >> something that is actually on the system and then
    >> restarts.
    >>
    >> What is the best way to check whether a given font
    > is
    >> on the system and return a different font if not?
    >> Would something like this be enough:
    >>
    >> NSString *defaultFontName = [[[NSFontManager
    >> sharedFontManager] availableFonts]
    >> containsObject:@"Optima"] ? @"Optima" :
    > @"Helvetica";
    >>
    >> Even then, what if the user has deleted
    > "Helvetica"?
    >> Is this safe or is there a better way?
    >>
    >> Thanks in advance,
    >> Keith
    >>
    >> __________________________________________________
    >> Do You Yahoo!?
    >> Tired of spam?  Yahoo! Mail has the best spam
    > protection around
    >> http://mail.yahoo.com
    >> _______________________________________________
    >> Do not post admin requests to the list. They will
    > be ignored.
    >> Cocoa-dev mailing list
    > (<Cocoa-dev...>)
    >> Help/Unsubscribe/Update your Subscription:
    >>
    >
    http://lists.apple.com/mailman/options/cocoa-dev/<dev...>
    >>
    >> This email sent to <dev...>
    >>
    >
    >

    __________________________________________________
    Do You Yahoo!?
    Tired of spam?  Yahoo! Mail has the best spam protection around
    http://mail.yahoo.com
  • On Mon, 2 Oct 2006 11:46:03 -0700 (PDT), Keith Blount
    <keithblount...> said:
    > Would something like this be enough:
    >
    > NSString *defaultFontName = [[[NSFontManager
    > sharedFontManager] availableFonts]
    > containsObject:@"Optima"] ? @"Optima" : @"Helvetica";

    No, you can get a false positive that way. See this thread:

    <http://www.cocoabuilder.com/archive/message/cocoa/2006/2/27/157571>

    m.

    --
    matt neuburg, phd = <matt...>, <http://www.tidbits.com/matt/>
    A fool + a tool + an autorelease pool = cool!
    AppleScript: the Definitive Guide - Second Edition!
    <http://www.amazon.com/gp/product/0596102119>
previous month october 2006 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