fontDescriptorWithFamily: method often doesn't produce a valid descriptor

  • Am I using NSFontDescriptor's fontDescriptorWithFamily mehtod
    correctly? I would like to substitute fonts in an attributed string
    with a font from a different family, but with the same typeface, etc.
    as the original font in the string.

    However, in the code below, newFont is often returned as nil...

    @implementation NSAttributedString(NSAttributedStringCategory)

    // Substitute a font family, keeping typeface, size, etc. unchanged.
    - (NSAttributedString*)stringBySubstitutingFontFamily:(NSString*)family
    {
    NSRange searchRange = {0, [self length]};
    NSRange fontRange;
    NSMutableAttributedString* newString = [[[NSMutableAttributedString
    alloc] initWithAttributedString:self] autorelease];

    while (searchRange.length > 0)
    {
      NSFont* oldFont = [self attribute:NSFontAttributeName
        atIndex:searchRange.location longestEffectiveRange:&fontRange
        inRange:searchRange];
      NSFontDescriptor* oldDescriptor = [oldFont fontDescriptor];
      NSFontDescriptor* newDescriptor = [oldDescriptor
    fontDescriptorWithFamily:family];
      float size = [oldFont pointSize];
      NSFont* newFont = [NSFont fontWithDescriptor:newDescriptor size:size];

      if (newFont) [newString setAttributes:[NSDictionary
    dictionaryWithObject:newFont forKey:NSFontAttributeName]
    range:fontRange];

      searchRange.location += fontRange.length;
      searchRange.length -= fontRange.length;
    }

    return newString;
    }

    @end
  • On Sep 3, 2007, at 11:19 PM, Dave Fernandes wrote:

    > Am I using NSFontDescriptor's fontDescriptorWithFamily mehtod
    > correctly? I would like to substitute fonts in an attributed string
    > with a font from a different family, but with the same typeface,
    > etc. as the original font in the string.

    Your code looks okay at first glance.

    I've had trouble converting fonts using NSFontDescriptor.  I had the
    same trouble as you -- I'd get nil back instead of a new font
    descriptor, even though my parameters seemed reasonable.

    I found that NSFontManager's conversion routines were more reliable
    (you probably want -convertFont:toFamily:).  It also seems like the
    better place to ask for font conversions, since you can subclass
    NSFontManager to perform your own brand of font conversion.

    Honestly, I'm still trying to figure out what NSFontDescriptor *is*
    good for, besides being another way to describe a font.

    --John
  • Thanks! NSFontManager did the trick.

    Dave

    On Sep 4, 2007, at 2:46 AM, John Labovitz wrote:

    > On Sep 3, 2007, at 11:19 PM, Dave Fernandes wrote:
    >
    >> Am I using NSFontDescriptor's fontDescriptorWithFamily mehtod
    >> correctly? I would like to substitute fonts in an attributed
    >> string with a font from a different family, but with the same
    >> typeface, etc. as the original font in the string.
    >
    > Your code looks okay at first glance.
    >
    > I've had trouble converting fonts using NSFontDescriptor.  I had
    > the same trouble as you -- I'd get nil back instead of a new font
    > descriptor, even though my parameters seemed reasonable.
    >
    > I found that NSFontManager's conversion routines were more reliable
    > (you probably want -convertFont:toFamily:).  It also seems like the
    > better place to ask for font conversions, since you can subclass
    > NSFontManager to perform your own brand of font conversion.
    >
    > Honestly, I'm still trying to figure out what NSFontDescriptor *is*
    > good for, besides being another way to describe a font.
    >
    > --John
    >
previous month september 2007 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
Go to today