Last active
August 29, 2015 14:00
-
-
Save nonstriater/11107653 to your computer and use it in GitHub Desktop.
普通字体转换成粗体
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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