Skip to content

Instantly share code, notes, and snippets.

@bishalg
Forked from kerrishotts/gist:5061352
Last active August 29, 2015 14:12
Show Gist options
  • Save bishalg/1aa5e73cdc390ca7d5c0 to your computer and use it in GitHub Desktop.
Save bishalg/1aa5e73cdc390ca7d5c0 to your computer and use it in GitHub Desktop.
// UIFont+Utility.h
// UIFont+Utility.[h|m] is (C) Kerri Shotts 2013, and released under an MIT license.
#import <UIKit/UIKit.h>
/**
*
* PKFontNormal = no modifications to the font; i.e., -Regular, -Roman, -Book, etc.
* PKFontBold = bold font desired (if possible); i.e., -Bold, -Black, -Heavy, etc.
* PKFontItalic = italic font desired (if possible); i.e., -Italic, -Oblique, etc.
*
*/
typedef enum {
PKFontNormal = 0,
PKFontBold = 1 << 0,
PKFontItalic = 1 << 1
} PKFontAttribute;
@interface UIFont (Utility)
/**
*
* Returns a string usable for [UIFont fontWithName:size:] based upon the supplied fontName
* and attributes.
*
* For example:
*
* [UIFont fontForFamilyName: @"Helvetica" withAttributes: PKFontBold] ==> "Helvetica-Bold"
*
* In addition, if the font name has the word "Bold" or "Italic" (or both), the attributes will
* be used as well, so:
*
* [UIFont fontForFamilyName: @"Helvetica Bold" withAttributes: PKFontNormal] ==> "Helvetica-Bold"
*
* If a font does not support the desired attributes, the nearest rendition is used. In this
* case if a font supports bold but not italic, and we request a bold, italic font, the result
* will be a bold font instead. Likewise, the reverse is true -- if a font only supports italic,
* and we request a bold, italic font, we'll get italic back instead.
*
* For fonts not supported by the mapping matrix, the original font name is returned (minus the
* "Bold" and "Italic" attributes). This is used to support "Symbol" and other fonts with only
* one style.
*
*/
+(NSString *)fontForFamilyName: (NSString *)fontName withAttributes: (PKFontAttribute)attributes;
/**
*
* Convenience method if you're not going to use the attributes parameter above.
*
*/
+(NSString *)fontForFamilyName: (NSString *)fontName;
/**
*
* Returns a font with the specified fontName (using the above fontForFamilyName:withAttributes method)
* and if the font is not found, uses the altFontName (using the same method above). If no font is yet
* found, Helvetica is returned.
*
* THIS MEANS THAT YOU WILL NEVER BE PRINTING TEXT WITH A NIL FONT! :-)
*
*/
+(UIFont *)fontWithName:(NSString *)fontName andSize:(CGFloat)fontSize usingFallback: (NSString *)altFontName;
/**
*
* If you have no desire to specify a fallback font, use this method instead.
* It's also nearly able to drop-in to your code -- just replace "size:" with "andSize:".
*
* For example:
*
* [UIFont fontWithName:@"Helvetica" size:20]
*
* becomes
*
* [UIFont fontWithName:@"Helvetica" andSize:20]
*
*/
+(UIFont *)fontWithName:(NSString *)fontName andSize:(CGFloat)fontSize;
/**
*
* Takes the family name of a font instance and tweaks it a bit
* to return a family name that is semi-orthogonal to the mapping
* table. Not quite, though, so expect some weirdness.
*
*/
-(NSString *)mappableFamilyName;
/**
*
* Returns YES if the font is italicized.
*
*/
-(BOOL) isItalicized;
/**
*
* Returns YES if the font is bolded.
*
*/
-(BOOL) isBolded;
/**
*
* Returns the bold equivalent of the font instance.
*
*/
-(UIFont *)boldFont;
/**
*
* Returns the italicized equivalent of the font instance.
*
*/
-(UIFont *)italicFont;
/**
*
* Returns the bolded and italicized equivalent of the font instance.
*
*/
-(UIFont *)boldItalicFont;
/**
*
* Returns a font with a new size that is larger or smaller than
* the original. For example a delta of 6 would return a font 6pts
* larger than the original.
*
*/
-(UIFont *)fontWithSizeDelta:(CGFloat)theDelta;
/**
*
* Returns a font with the size multiplied by the Delta Percent.
* For example, if the value is 1.25, the font will be 1.25 times
* larger than the original.
*
*/
-(UIFont *)fontWithSizeDeltaPercent:(CGFloat)theDeltaPercent;
@end
// UIFont+Utility.m
// UIFont+Utility.[h|m] is (C) Kerri Shotts 2013, and released under an MIT license.
#import "UIFont+Utility.h"
@implementation UIFont (Utility)
+(NSString *)fontForFamilyName: (NSString *)fontName
withAttributes: (PKFontAttribute)attributes
{
PKFontAttribute fuzzyAttributes = attributes;
if ( [fontName rangeOfString:@"Bold"].location != NSNotFound ) fuzzyAttributes |= PKFontBold;
if ( [fontName rangeOfString:@"Italic"].location != NSNotFound ) fuzzyAttributes |= PKFontItalic;
NSString *theNewFontName = [[fontName stringByReplacingOccurrencesOfString:@"Bold" withString:@""]
stringByReplacingOccurrencesOfString:@"Italic" withString:@""];
theNewFontName = [theNewFontName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
static NSDictionary *fontMap;
if (!fontMap)
{
fontMap = @{ //family //substitutionary string //normal //bold //italic //bold-italic
@"American Typewriter Light": @[ @"AmericanTypewriter%@", @"-Light", @"", @"-Light", @""],
@"American Typewriter": @[ @"AmericanTypewriter%@", @"", @"-Bold", @"", @"-Bold" ],
@"Arial": @[ @"Arial%@MT", @"", @"-Bold", @"-Italic", @"-BoldItalic" ],
@"Avenir Light": @[ @"Avenir%@", @"-Light", @"-Roman", @"-LightOblique", @"-Oblique" ],
@"Avenir": @[ @"Avenir%@", @"-Roman", @"-Heavy", @"-Oblique", @"-HeavyOblique" ],
@"Avenir Heavy": @[ @"Avenir%@", @"-Heavy", @"-Black", @"-HeavyOblique", @"-BlackOblique" ],
@"Avenir Next Light": @[ @"AvenirNext%@", @"-UltraLight", @"-Regular", @"-UltraLightItalic", @"-Italic" ],
@"Avenir Next": @[ @"AvenirNext%@", @"-Regular", @"-Bold", @"-Italic", @"-BoldItalic" ],
@"Avenir Next Heavy": @[ @"AvenirNext%@", @"-Bold", @"-Heavy", @"-BoldItalic", @"-HeavyItalic" ],
@"Baskerville": @[ @"Baskerville%@", @"", @"-Bold", @"-Italic", @"-BoldItalic" ],
@"Bodoni 72": @[ @"BodoniSvtyTwoITCTT%@", @"-Book", @"-Bold", @"-BookIta", @"-Bold" ],
@"Bodoni 72 Oldstyle": @[ @"BodoniSvtyTwoOSITCTT%@", @"-Book", @"-Bold", @"-BookIt", @"-Bold" ],
@"Chalkboard Light": @[ @"ChalkboardSE%@", @"-Light", @"-Regular", @"-Light", @"-Regular" ],
@"Chalkboard": @[ @"ChalkboardSE%@", @"-Regular", @"-Bold", @"-Regular", @"-Bold" ],
@"Cochin": @[ @"Cochin%@", @"", @"-Bold", @"-Italic", @"-BoldItalic" ],
@"Copperplate Light": @[ @"Copperplate%@", @"-Light", @"", @"-Light", @"" ],
@"Copperplate": @[ @"Copperplate%@", @"", @"-Bold", @"", @"-Bold" ],
@"Courier": @[ @"Courier%@", @"", @"-Bold", @"-Oblique", @"-BoldOblique" ],
@"Courier New": @[ @"CourierNewPS%@MT", @"", @"-Bold", @"-Italic", @"-BoldItalic" ],
@"Didot": @[ @"Didot%@", @"", @"-Bold", @"-Italic", @"-Bold" ],
@"Euphemia UCAS": @[ @"EuphemiaUCAS%@", @"", @"-Bold", @"-Italic", @"-Bold" ],
@"Futura Medium": @[ @"Futura-Medium%@", @"", @"", @"Italic", @"Italic" ],
@"Geeza Pro": @[ @"GeezaPro%@", @"", @"-Bold", @"", @"-Bold" ],
@"Georgia": @[ @"Georgia%@", @"", @"-Bold", @"-Italic", @"-BoldItalic" ],
@"Gill Sans Light": @[ @"GillSans%@", @"-Light", @"", @"-LightItalic", @"-Italic" ],
@"Gill Sans": @[ @"GillSans%@", @"", @"-Bold", @"-Italic", @"-BoldItalic" ],
@"Helvetica Light": @[ @"Helvetica%@", @"-Light", @"", @"-LightOblique", @"-Oblique" ],
@"Helvetica": @[ @"Helvetica%@", @"", @"-Bold", @"-Oblique", @"-BoldOblique" ],
@"Helvetica Neue Light": @[ @"HelveticaNeue%@", @"-Light", @"", @"-LightItalic", @"-Italic" ],
@"Helvetica Neue": @[ @"HelveticaNeue%@", @"", @"-Bold", @"-Italic", @"-BoldItalic" ],
@"Hoefler Text": @[ @"HoeflerText%@", @"-Regular", @"-Black", @"-Italic", @"-BlackItalic" ],
@"Marion": @[ @"Marion%@", @"-Regular", @"-Bold", @"-Italic", @"-Bold" ],
@"Marker Felt": @[ @"MarkerFelt%@", @"-Thin", @"-Wide", @"-Thin", @"-Wide" ],
@"Noteworthy": @[ @"Noteworthy%@", @"-Light", @"-Bold", @"-Light", @"-Bold" ],
@"Open Dyslexic": @[ @"OpenDyslexic%@", @"-Regular", @"-Bold", @"-Italic", @"-BoldItalic" ],
@"Optima": @[ @"Optima%@", @"-Regular", @"-Bold", @"-Italic", @"-BoldItalic" ],
@"Palatino": @[ @"Palatino%@", @"-Roman", @"-Bold", @"-Italic", @"-BoldItalic" ],
@"Snell Roundhand": @[ @"SnellRoundhand%@", @"", @"-Bold", @"", @"-Bold" ],
@"Times New Roman": @[ @"TimesNewRomanPS%@MT", @"", @"-Bold", @"-Italic", @"-BoldItalic" ],
@"Trebuchet MS": @[ @"TrebuchetMS%@", @"", @"-Bold", @"-Italic", @"-BoldItalic" ],
@"Verdana": @[ @"Verdana%@", @"", @"-Bold", @"-Italic", @"-BoldItalic" ]
};
}
NSArray *theFont = [fontMap objectForKey:theNewFontName];
if (theFont)
{
NSString *theSubstitutionary = theFont[0];
NSString *theSubstitionText = theFont[ (NSUInteger)fuzzyAttributes + 1 ];
return [NSString stringWithFormat:theSubstitutionary, theSubstitionText];
}
else
{
// return the original -- just in case (for things like Symbol)
return fontName;
}
}
+(NSString *)fontForFamilyName: (NSString *)fontName
{
return [UIFont fontForFamilyName:fontName withAttributes:PKFontNormal];
}
+(UIFont *)fontWithName:(NSString *)fontName andSize:(CGFloat)fontSize usingFallback: (NSString *)altFontName
{
UIFont *theFont = [UIFont fontWithName: [UIFont fontForFamilyName:fontName] size:fontSize];
if (!theFont)
{
theFont = [UIFont fontWithName: [UIFont fontForFamilyName:altFontName] size:fontSize];
}
if (!theFont)
{
theFont = [UIFont fontWithName: [UIFont fontForFamilyName:@"Helvetica"] size:fontSize];
}
return theFont;
}
+(UIFont *)fontWithName:(NSString *)fontName andSize:(CGFloat)fontSize
{
return [UIFont fontWithName:fontName andSize:fontSize usingFallback:@"Helvetica"];
}
-(NSString *)mappableFamilyName
{
NSString *theFamilyName = self.familyName;
if ([self.fontName rangeOfString:@"Light"].location != NSNotFound)
{
theFamilyName = [theFamilyName stringByAppendingString: @" Light"];
}
return theFamilyName;
}
-(BOOL) isItalicized
{
return [self.fontName rangeOfString:@"Italic"].location != NSNotFound ||
[self.fontName rangeOfString:@"Oblique"].location != NSNotFound ||
[self.fontName rangeOfString:@"BookIt"].location != NSNotFound; // Bodoni.
}
-(BOOL) isBolded
{
return [self.fontName rangeOfString:@"Bold"].location != NSNotFound ||
[self.fontName rangeOfString:@"Heavy"].location != NSNotFound ||
[self.fontName rangeOfString:@"Black"].location != NSNotFound;
}
-(UIFont *)boldFont
{
if (self.isItalicized)
{
return [UIFont fontWithName:[ self.mappableFamilyName stringByAppendingString: @" Bold Italic" ] andSize:self.pointSize
usingFallback: self.fontName];
}
else
{
return [UIFont fontWithName:[ self.mappableFamilyName stringByAppendingString: @" Bold" ] andSize:self.pointSize usingFallback: self.fontName];
}
}
-(UIFont *)italicFont
{
if (self.isBolded)
{
return [UIFont fontWithName:[ self.mappableFamilyName stringByAppendingString: @" Bold Italic" ] andSize:self.pointSize usingFallback: self.fontName];
}
else
{
return [UIFont fontWithName:[ self.mappableFamilyName stringByAppendingString: @" Italic" ] andSize:self.pointSize usingFallback: self.fontName];
}
}
-(UIFont *)boldItalicFont
{
return [UIFont fontWithName:[ self.mappableFamilyName stringByAppendingString: @" Bold Italic" ] andSize:self.pointSize usingFallback: self.fontName];
}
-(UIFont *)fontWithSizeDelta:(CGFloat)theDelta
{
return [self fontWithSize:self.pointSize + theDelta];
}
-(UIFont *)fontWithSizeDeltaPercent:(CGFloat)theDeltaPercent
{
return [self fontWithSize:self.pointSize * theDeltaPercent];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment