Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save holysin/8419473 to your computer and use it in GitHub Desktop.
Save holysin/8419473 to your computer and use it in GitHub Desktop.
Get the variation of the UIFont
UIFont * GetVariationOfFontWithTrait(UIFont *baseFont, CTFontSymbolicTraits trait) {
CGFloat fontSize = [baseFont pointSize];
CFStringRef baseFontName = (__bridge CFStringRef)[baseFont fontName];
CTFontRef baseCTFont = CTFontCreateWithName(baseFontName, fontSize, NULL);
CTFontRef ctFont = CTFontCreateCopyWithSymbolicTraits(baseCTFont, 0, NULL, trait, trait);
NSString *variantFontName = CFBridgingRelease(CTFontCopyName(ctFont, kCTFontPostScriptNameKey));
UIFont *variantFont = [UIFont fontWithName:variantFontName size:fontSize];
CFRelease(ctFont);
CFRelease(baseCTFont);
return variationFont;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment