iOS适配字体大小(自己常用的宏定义)

在项目中肯定会遇到字体适配的问题,从上面可以看出:一般情况是在4,5,6上用一套字体大小,在6P上字体大小扩大1.5倍数。(淘宝字体是这样子的)

方法一:用宏定义适配字体大小(根据手机尺寸判断)

1.5代表6P尺寸的时候字体为1.5倍,5S和6尺寸时大小一样,也可根据需求自定义比例。

代码如下:

#define IsIphone6P          SCREEN_WIDTH==414
#define SizeScale           (IsIphone6P ? 1.5 : 1)
#define kFontSize(value)    value*SizeScale
#define kFont(value)        [UIFont systemFontOfSize:value*SizeScale]

方法二:用宏定义适配字体大小(根据手机尺寸判断)


//不同屏幕尺寸字体适配
#define kScreenWidthRatio  (UIScreen.mainScreen.bounds.size.width / 375.0)
#define kScreenHeightRatio (UIScreen.mainScreen.bounds.size.height / 667.0)
#define AdaptedWidth(x)  ceilf((x) * kScreenWidthRatio)
#define AdaptedHeight(x) ceilf((x) * kScreenHeightRatio)
#define AdaptedFontSize(R)     [UIFont systemFontOfSize:AdaptedWidth(R)]

项目常用宏

/* ********************************************************************* */
#ifdef DEBUG

#define NSLog( s, ... ) NSLog( @"< %@:(%d) > %@", [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] )

#else

#define NSLog( s, ... )

#endif
/* ********************************************************************* */
// 判断是否为iPhone X
#define iPhoneX [[UIScreen mainScreen] bounds].size.width == 375.0f && [[UIScreen mainScreen] bounds].size.height == 812.0f

#define KWIDTH   [UIScreen mainScreen].bounds.size.width
#define KHEIGHT  [UIScreen mainScreen].bounds.size.height

//不同屏幕尺寸字体适配
#define kScreenWidthRatio  (UIScreen.mainScreen.bounds.size.width / 375.0)
#define kScreenHeightRatio (UIScreen.mainScreen.bounds.size.height / 667.0)
#define AdaptedWidth(x)  ceilf((x) * kScreenWidthRatio)
#define AdaptedHeight(x) ceilf((x) * kScreenHeightRatio)
#define AdaptedFontSize(R)     [UIFont systemFontOfSize:AdaptedWidth(R)]

// iPhone X  (安全区域 734 = 812 - kStatusBarHeight - KIndicatorHeight)
// 状态栏高度
#define kStatusBarHeight        [[UIApplication sharedApplication] statusBarFrame].size.height
// tabBar高度
#define kBottomBarHeight        (iPhoneX ? 83.f : 49.f)
// 导航栏高度
#define kNavigationBarHeight    (iPhoneX ? 88.f : 64.f)
// home indicator
#define KIndicatorHeight        (iPhoneX ? 34.f : 0.f)

/* ********************************************************************* */

/**
 * View 圆角和加边框
 */
#define KViewBorderRadius(View, Radius, Width, Color)\
\
[View.layer setCornerRadius:(Radius)];\
[View.layer setMasksToBounds:YES];\
[View.layer setBorderWidth:(Width)];\
[View.layer setBorderColor:[Color CGColor]]

/**
 * View 圆角
 */
#define KViewRadius(View, Radius)\
\
[View.layer setCornerRadius:(Radius)];\
[View.layer setMasksToBounds:YES]

/* ********************************************************************* */

你可能感兴趣的:(iOS适配字体大小(自己常用的宏定义))