0

I'm currently trying to override the systemFontOfSize method of UIFont to return my custom font with the given font size. My code currently looks like this:

extension UIFont
{
    class func systemFontOfSize(fontSize: CGFloat) -> UIFont
    {
        return UIFont(name: "HelveticaNeue", size: 10)!
    }
}

However, I am unable to override that method. When using the code shown above, I get the following error:

"Method 'systemFontOfSize' with Objective-C selector 'systemFontOfSize:' conflicts with previous declaration with the same Objective-C selector"

When adding override, the error is that the method is not overriding anything. Does anyone know how to fix this issue?

Cristik
  • 30,989
  • 25
  • 91
  • 127
user1945317
  • 107
  • 9

5 Answers5

1

You can use Objective C extension as follows, if it fits your needs.

Earlier discussion related to this technique with my source code as an answer can be found here: Is there a way to change default font for your application.

All you have to do is to implement the class with the code mentioned at the link above and include the following header file in your umbrella header to be able to make the code be applied when calling methods in Swift code.

Header file UIFont+Utils.h:

@interface UIFont (Utils)

+ (UIFont *)systemFontOfSize:(CGFloat)size;
+ (UIFont *)lightSystemFontOfSize:(CGFloat)size;
+ (UIFont *)boldSystemFontOfSize:(CGFloat)size;
+ (UIFont *)preferredFontForTextStyle:(NSString *)style;

@end
Community
  • 1
  • 1
Michi
  • 1,464
  • 12
  • 15
1

You can use the #selector to redirect UIFont calls go to custom functions.

For best way how to replace font functions for whole iOS app, please check the below SO question, look for answer from - Javier Amor Penas: Set a default font for whole iOS app?
0

to be able to override the function, you must first subclass UIFont. what are you trying to do, is impossible. you are trying to redefine function in extension. so, i have no positive message, but that is the reality.

PS: if you are an Objective C 'expert', some hack could be available ...

user3441734
  • 16,722
  • 2
  • 40
  • 59
  • I was afraid this might be the case :( – user1945317 Jan 04 '16 at 10:24
  • @user1945317 OK, did you try to check UIAppearance? Use the UIAppearance protocol to get the appearance proxy for a class. You can customize the appearance of instances of a class by sending appearance modification messages to the class’s appearance proxy. – user3441734 Jan 04 '16 at 10:31
0

class FontClass: UIFont{ override class func systemFontOfSize(fontSize: CGFloat) -> UIFont { return UIFont(name: "HelveticaNeue", size: 10)! } } In the project, you can use it like this: FontClass.systemFontOfSize(10) The hope can help you!

0

From Apple's documentation on extensions:

NOTE

Extensions can add new functionality to a type, but they cannot override existing functionality.

You cannot overwrite existing methods in an extension, you can only add new ones.

Community
  • 1
  • 1
Cristik
  • 30,989
  • 25
  • 91
  • 127
  • If there's a different way to overwrite existing methods so that it uses the custom font for everything, including tableview headers and labels placed in storyboard, that would also be fine. – user1945317 Jan 04 '16 at 10:25
  • You can do this via `UIAppearance`. See [this](http://stackoverflow.com/questions/8707082/set-a-default-font-for-whole-ios-app) SO question on this, or [this article](http://nscookbook.com/2013/01/ios-programming-recipe-8-using-uiappearance-for-a-custom-look/) – Cristik Jan 04 '16 at 10:34
  • Problem with that solution is that it sets the size for everything to the same size – user1945317 Jan 04 '16 at 10:42
  • No :) It just set everything to my font instead of the system font. Code basically looks like this: + (UIFont *)systemFontOfSize:(CGFloat)fontSize { return [UIFont fontWithName:[[AppTheme sharedInstance].fontFamilyName stringByAppendingString:@"-regular"] size:fontSize]; } – user1945317 Jan 06 '16 at 08:57