3
votes

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!

4

4 Answers

8
votes

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
0
votes

Just check fontName and/or familyName?

NSLog(@"Fontname: %@", systemFont1.fontName);
NSLog(@"Familyname: %@", systemFont1.familyName);
0
votes

I have created method and tested in both iOS 6 and iOS7. It is working fine. I was getting result what you are expected.

Here is Code:

- (BOOL)isSystemFont:(UIFont*)pFont {
    NSLog(@"pFont-%@",pFont.familyName);

    // Object created for comparing system font with custom font
    UIFont *font = [UIFont systemFontOfSize:10];
    NSLog(@"Font-%@",font.familyName);

    if ([pFont.familyName isEqualToString:font.familyName]) {
        return YES;
    } else {
        return NO;
    }
}

Hope, It will may helpful to you.

0
votes

Fonts consist of more than name and size. Here's a more thorough check. Caveat: only work on iOS 7.

BOOL FontIsEqualToFont(UIFont *font1, UIFont *font2)
{
    return [[[font1 fontDescriptor] fontAttributes] isEqual:[[font2 fontDescriptor] fontAttributes]];
}

// use as follows:
BOOL isSystemFont = FontIsEqualToFont(systemFont1, customFont1);

Another good way to uniquely identify fonts is to use their postscript names:

BOOL fontsAreEqual = [[[font1 fontDescriptor] postScriptName] isEqualToString:[[font2 fontDescriptor] postScriptName]];