在項(xiàng)目中肯定會遇到字體適配的問題袋毙,從上面可以看出:一般情況是在4,5冗尤,6上用一套字體大小听盖,在6P上字體大小擴(kuò)大1.5倍數(shù)。(淘寶字體是這樣子的)
方法一:用宏定義適配字體大辛哑摺(根據(jù)手機(jī)尺寸判斷)
1.5代表6P尺寸的時(shí)候字體為1.5倍皆看,5S和6尺寸時(shí)大小一樣,也可根據(jù)需求自定義比例背零。
代碼如下:
#define IsIphone6P SCREEN_WIDTH==414
#define SizeScale (IsIphone6P ? 1.5 : 1)
#define kFontSize(value) value*SizeScale
#define kFont(value) [UIFont systemFontOfSize:value*SizeScale]
方法二:用宏定義適配字體大醒鳌(根據(jù)手機(jī)尺寸判斷)
//不同屏幕尺寸字體適配
#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)]
項(xiàng)目常用宏
/* ********************************************************************* */
#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 (安全區(qū)域 734 = 812 - kStatusBarHeight - KIndicatorHeight)
// 狀態(tài)欄高度
#define kStatusBarHeight [[UIApplication sharedApplication] statusBarFrame].size.height
// tabBar高度
#define kBottomBarHeight (iPhoneX ? 83.f : 49.f)
// 導(dǎo)航欄高度
#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]
/* ********************************************************************* */