我想在我的应用程序中扩展UIFont类,这样我就可以使用大/中/小字母的概念。
我创造了但出于某种原因它看起来很奇怪。我完成了它,它做了应该做的事情,但我对它不满意。通常当我有这种感觉的时候,是因为我搞砸了,或者只是个新手。也许我使用的常量是多少?
这段代码在专业上可以接受吗?我在这里寻找的是来自这里有经验的开发人员的输入。
#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发布于 2016-06-29 01:08:21
看起来不错,不过我会用苹果大会上的名字systemFontOfSizeSmall或systemFontSmall。
发布于 2016-08-05 03:50:14
我会为此做一个宏:
//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)
...
@endhttps://codereview.stackexchange.com/questions/133334
复制相似问题