Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if UIFont is system font?

I have the following code for that creates multiple fonts.

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];

I would like to know which UIFont's are system. I practically need a method that would return a BOOL YES for variables: systemFont1, systemFont2, systemFont3 and NO for customFont4, customFont5, customFont6.

Since Helvetica Neue is system font on iOS7, this is subject of a debate, whether it should return NO or YES in these cases, but for my issue, it would be fine either way.

So my question is:

How to verify if UIFont instance was created by either of system font methods?

Thank you for your help!

like image 574
Legoless Avatar asked Sep 16 '25 11:09

Legoless


1 Answers

Here is your method you want:

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

Or as an extension in Swift3

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

Above method will return as you need

if([self isSystemFont:systemFont1]) NSLog(@"SystemFont");
else NSLog(@"Custom Font");
if([self isSystemFont:customFont1]) NSLog(@"SystemFont");
else NSLog(@"Custom Font");

Output is

2014-03-04 15:48:18.791 TestProject[4031:70b] SystemFont
2014-03-04 15:48:18.791 TestProject[4031:70b] Custom Font
like image 90
Tapas Pal Avatar answered Sep 19 '25 04:09

Tapas Pal