首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIFont展开

UIFont展开
EN

Code Review用户
提问于 2016-06-28 21:54:00
回答 2查看 89关注 0票数 2

我想在我的应用程序中扩展UIFont类,这样我就可以使用大/中/小字母的概念。

我创造了但出于某种原因它看起来很奇怪。我完成了它,它做了应该做的事情,但我对它不满意。通常当我有这种感觉的时候,是因为我搞砸了,或者只是个新手。也许我使用的常量是多少?

这段代码在专业上可以接受吗?我在这里寻找的是来自这里有经验的开发人员的输入。

UIFont+AppFonts.h

代码语言:javascript
复制
#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
EN

回答 2

Code Review用户

回答已采纳

发布于 2016-06-29 01:08:21

看起来不错,不过我会用苹果大会上的名字systemFontOfSizeSmallsystemFontSmall

票数 0
EN

Code Review用户

发布于 2016-08-05 03:50:14

我会为此做一个宏:

代码语言:javascript
复制
//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
票数 1
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/133334

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档