Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

_______________________________________________

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:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.