本文介绍了检测iOS上的字体是否为粗体/斜体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于UIFont或CTFont,如何判断字体是粗体还是斜体?

Given a UIFont or a CTFont, how can I tell whether the font is bold/italic?

推荐答案

查看字体的名称并不总是有效。考虑字体Courier Oblique(斜体)或HoeflerText-Black(粗体),其名称中都不包含粗体或斜体。

Looking at the font's name won't always work. Consider the font "Courier Oblique" (which is italic) or "HoeflerText-Black" (which is bold), Neither of those contain "bold" or "italic" in their names.

如果字体为 CTFontRef ,确定它是粗体还是斜体的正确方法是使用 CTFontGetSymbolicTraits function:

Given a font as a CTFontRef, the proper way to determine whether it's bold or italic is to use the CTFontGetSymbolicTraits function:

CTFontRef font = CTFontCreateWithName((CFStringRef)@"Courier Oblique", 10, NULL);
CTFontSymbolicTraits traits = CTFontGetSymbolicTraits(font);
BOOL isItalic = ((traits & kCTFontItalicTrait) == kCTFontItalicTrait);
BOOL isBold = ((traits & kCTFontBoldTrait) == kCTFontBoldTrait);
NSLog(@"Italic: %i Bold: %i", isItalic, isBold);
CFRelease(font);

这篇关于检测iOS上的字体是否为粗体/斜体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:30