如何检查UIFont是否是系统字体?

Leg*_*ess 2 iphone fonts uikit uifont ios

我有以下代码创建多个字体.

UIFont* systemFont1 = [UIFont systemFontOfSize:12.0];
UIFont* systemFont2 = [UIFont boldSystemFontOfSize:12.0];
UIFont* systemFont3 = [UIFont italicSystemFontOfSize:12.0];

UIFont* customFont1 = [UIFont fontWithName:@"HelveticaNeue-Light" size:12.0];
UIFont* customFont2 = [UIFont fontWithName:@"HelveticaNeue-Regular" size:12.0];
UIFont* customFont3 = [UIFont fontWithName:@"HelveticaNeue-Thin" size:12.0];

UIFont* customFont4 = [UIFont fontWithName:@"MyriadPro" size:12.0];
UIFont* customFont5 = [UIFont fontWithName:@"MyriadPro-Italic" size:12.0];
UIFont* customFont6 = [UIFont fontWithName:@"MyriadPro-Condensed" size:12.0];
Run Code Online (Sandbox Code Playgroud)

我想知道哪个UIFont是系统.我几乎需要将返回的方法BOOL YES变量:systemFont1,systemFont2,systemFont3和NOcustomFont4,customFont5,customFont6.

由于Helvetica Neue是iOS7上的系统字体,因此无论是应该返回NO还是YES在这些情况下都会引起争议,但对于我的问题,无论哪种方式都可以.

所以我的问题是:

如何验证UIFont实例是否是由系统字体方法之一创建的?

谢谢您的帮助!

Tap*_*Pal 6

这是你想要的方法:

-(BOOL)isSystemFont:(UIFont *)font
{
    return ([[font familyName] isEqualToString:[[UIFont systemFontOfSize:12.0f] familyName]])?YES:NO;
}
Run Code Online (Sandbox Code Playgroud)

或者作为Swift3的扩展

extension UIFont {
    func isSystemFont() -> Bool {
        return self.familyName == UIFont.systemFont(ofSize: 12.0).familyName
    }
}
Run Code Online (Sandbox Code Playgroud)

以上方法将根据您的需要返回

if([self isSystemFont:systemFont1]) NSLog(@"SystemFont");
else NSLog(@"Custom Font");
if([self isSystemFont:customFont1]) NSLog(@"SystemFont");
else NSLog(@"Custom Font");
Run Code Online (Sandbox Code Playgroud)

输出是

2014-03-04 15:48:18.791 TestProject[4031:70b] SystemFont
2014-03-04 15:48:18.791 TestProject[4031:70b] Custom Font
Run Code Online (Sandbox Code Playgroud)