• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
fontDescriptorWithFamily: method often doesn't produce a valid descriptor
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

fontDescriptorWithFamily: method often doesn't produce a valid descriptor


  • Subject: fontDescriptorWithFamily: method often doesn't produce a valid descriptor
  • From: Dave Fernandes <email@hidden>
  • Date: Tue, 4 Sep 2007 02:19:15 -0400

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

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: fontDescriptorWithFamily: method often doesn't produce a valid descriptor
      • From: John Labovitz <email@hidden>
  • Prev by Date: Re: How to de-register value observers
  • Next by Date: Re: fontDescriptorWithFamily: method often doesn't produce a valid descriptor
  • Previous by thread: making a button rotating (again)
  • Next by thread: Re: fontDescriptorWithFamily: method often doesn't produce a valid descriptor
  • Index(es):
    • Date
    • Thread