2
\$\begingroup\$

I wanted to expand the UIFont class in my application so I could use the large/medium/small letters concept.

I created but for some reason it looks weird. I finished it and it does what's is supposed to do but I am not happy with it. Usually when I have that feeling is because I am messing up big or just being a newbie. Maybe is the amount of constants I am using?

Is this code professionally acceptable? What I am looking for here is the input from the experienced developers out here.

UIFont+AppFonts.h

#import "UIFont+AppFonts.h"

//Contants for cohen ratios.
static const CGFloat cohenRatioSmall = 0.2f;
static const CGFloat cohenRatioMedium = 0.5f;
static const CGFloat cohenRatioLarge = 0.8f;

//Relationship calculation
static const CGFloat fontBaseSize = 34.0f;
static const CGFloat fontSmall = fontBaseSize * cohenRatioSmall;
static const CGFloat fontMedium = fontBaseSize * cohenRatioMedium;
static const CGFloat fontLarge = fontBaseSize * cohenRatioLarge;

//Static variables for the fonts.
static UIFont *DefaultFontSmall = nil;
static UIFont *DefaultFontMedium = nil;
static UIFont *DefaultFontLarge = nil;

static UIFont *DefaultFontBoldSmall = nil;
static UIFont *DefaultFontBoldMedium = nil;
static UIFont *DefaultFontBoldLarge = nil;


@implementation UIFont (AppFonts)

+ (UIFont *) defaultFontSmall { return DefaultFontSmall ?: (DefaultFontSmall= [UIFont systemFontOfSize:fontSmall]);}
+ (UIFont *) defaultFontMedium { return DefaultFontMedium ?: (DefaultFontMedium = [UIFont systemFontOfSize:fontMedium]);}
+ (UIFont *) defaultFontLarge { return DefaultFontLarge ?: (DefaultFontLarge = [UIFont systemFontOfSize:fontLarge]);}
+ (UIFont *) defaultFontBoldSmall { return DefaultFontBoldSmall ?: (DefaultFontBoldSmall = [UIFont boldSystemFontOfSize:fontSmall]);}
+ (UIFont *) defaultFontBoldMedium { return DefaultFontBoldMedium ?: (DefaultFontBoldMedium = [UIFont boldSystemFontOfSize:fontMedium]);}
+ (UIFont *) defaultFontBoldLarge { return DefaultFontBoldLarge ?: (DefaultFontBoldLarge = [UIFont boldSystemFontOfSize:fontLarge]);}

@end
\$\endgroup\$

2 Answers 2

0
\$\begingroup\$

Looks good, I would use names following the apple convention systemFontOfSizeSmall or systemFontSmall though.

\$\endgroup\$
1
  • \$\begingroup\$ Excellent idea. I changed that. \$\endgroup\$
    – user110128
    Jun 29, 2016 at 18:08
1
\$\begingroup\$

I would make a macro for that:

//Contants for cohen ratios.
static const CGFloat cohenRatioSmall = 0.2f;
static const CGFloat cohenRatioMedium = 0.5f;
static const CGFloat cohenRatioLarge = 0.8f;

//Relationship calculation
static const CGFloat fontBaseSize = 34.0f;
static const CGFloat fontSmall = fontBaseSize * cohenRatioSmall;
static const CGFloat fontMedium = fontBaseSize * cohenRatioMedium;
static const CGFloat fontLarge = fontBaseSize * cohenRatioLarge;

#define DEFINE_SYSTEM_FONT(name, size) \
    + (UIFont *)name { \
        static UIFont *font; \
        return font ?: (font = [UIFont systemFontOfSize:size]); \
    }

#define DEFINE_BOLD_SYSTEM_FONT(name, size) \
    + (UIFont *)name { \
        static UIFont *font; \
        return font ?: (font = [UIFont boldSystemFontOfSize:size]); \
    }


@implementation UIFont (AppFonts)

DEFINE_SYSTEM_FONT(defaultFontSmall, fontSmall)
...

@end
\$\endgroup\$
1
  • \$\begingroup\$ Greetings, welcome to CodeReview. This is not really a CodeReview answer, just a full scale rewrite of the OP's code. If it was not for 'I would make a macro for that' this would have been -1'd. You clearly put some effort in so I gave you a +1 in the hope that you will become a valuable member providing CodeReviews ;) \$\endgroup\$
    – konijn
    Aug 5, 2016 at 10:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.