fontDescriptorWithFamily: method often doesn't produce a valid descriptor
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