How do I create a bold UIFont from a regular UIFont?

后端 未结 7 1902
醉话见心
醉话见心 2020-12-08 19:08

If I have a UIFont object, is it possible to convert it to bold? I don\'t know the font name, I just have a UIFont object. What I want is a function like

<
相关标签:
7条回答
  • 2020-12-08 20:02

    Attempting to derive the bold/italic font using font or family names no longer works correctly since iOS 7, due to the cryptic font family name of the system font. Below is a simple extension to derive the bold/italic font using the UIFontDescriptor class.

    +(UIFont *) font:(UIFont *)font bold:(BOOL)bold italic:(BOOL)italic
    {
        NSUInteger traits = 0;
        if (bold)
        {
            traits |= UIFontDescriptorTraitBold;
        }
        if (italic)
        {
            traits |= UIFontDescriptorTraitItalic;
        }
        return [UIFont fontWithDescriptor:[[font fontDescriptor] fontDescriptorWithSymbolicTraits:traits] size:font.pointSize];
    }
    
    0 讨论(0)
提交回复
热议问题