Skip to content

Instantly share code, notes, and snippets.

@nonstriater
Last active August 29, 2015 14:00
Show Gist options
  • Save nonstriater/11107653 to your computer and use it in GitHub Desktop.
Save nonstriater/11107653 to your computer and use it in GitHub Desktop.
普通字体转换成粗体
// Returns the bold version of a font. May return nil if there is no bold version.
UIFont *RNGetBoldFontForFont(UIFont *font) {
UIFont *result = nil;
CTFontRef ctFont = CTFontCreateWithName((__bridge CFStringRef)(font.fontName),
font.pointSize, NULL);
if (ctFont) {
// You can't add bold to a bold font
// (don't really need this, since the ctBoldFont check would handle it)
if ((CTFontGetSymbolicTraits(ctFont) & kCTFontTraitBold) == 0) {
CTFontRef ctBoldFont = CTFontCreateCopyWithSymbolicTraits(ctFont,
font.pointSize,
NULL,
kCTFontBoldTrait,
kCTFontBoldTrait);
if (ctBoldFont) {
NSString *fontName = CFBridgingRelease(CTFontCopyPostScriptName(ctBoldFont));
result = [UIFont fontWithName:fontName size:font.pointSize];
CFRelease(ctBoldFont);
}
}
CFRelease(ctFont);
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment