App開發流程之通用宏定義及標頭檔,app
工欲善其事,必先利其器。
在正式實現各種炫酷的功能和UI前,做好準備工作是提高後續開發效率的必經之路。
所以,這個系列,我不是在各種堆技術,更關注的是“兵馬動”之前的“糧草行”,有些繁瑣,但當清晰理出整個脈絡,後續的工作只是在良好的基礎和架構上無限擴充和最佳化。
宏定義,是開發過程中提高效率的有效工具。很有必要歸納一些全域通用的宏定義,以便簡單高效的使用。
如下,整理了一些通用宏定義,應該是開發必備的:
#pragma mark -- 設定全域尺寸宏#define StatusBarHeight 20#define NaviBarHeight 44#define TabBarHeight 49#define KeyboardEngHeight 216#define KeyboardHansHeight 252#pragma mark -- 設定全域顏色宏//下列宏定義,只作為一種裝置適配參考方案#define StatusBarColor [UIColor blackColor]#define NaviBarColor [UIColor whiteColor]#define NaviBarTitleSelectedColor COLOR(255, 120, 100)#define NaviBarTitleUnselectedColor COLOR(100, 100, 100)#define NaviBarShadowColor COLOR(230, 230, 230)#define NaviBarTitleFontSize ((DeviceIsNotRetina || DeviceIsiPhone4s || DeviceIsiPhone5) ? 17 : 19)#define NaviBarTitleAttributes [NSDictionary dictionaryWithObjectsAndKeys:FONTAppliedFixed(NaviBarTitleFontSize),NSFontAttributeName,NaviBarTitleUnselectedColor, NSForegroundColorAttributeName, nil]#define NaviItemTextFontSize ((DeviceIsNotRetina || DeviceIsiPhone4s || DeviceIsiPhone5) ? 14 : 16)#define NaviItemTextGrayColor COLOR(140, 140, 140)#define NaviBottomLineColor NaviBarTitleSelectedColor#pragma mark -- 字串本地化宏#define LocalizedString(String) NSLocalizedString(String,String)#pragma mark -- 擷取裝置寬度和高度//會隨著應用內部橫屏和豎屏變化#define DeviceSize [UIScreen mainScreen].bounds.size#define DeviceWidth DeviceSize.width#define DeviceHeight DeviceSize.height#pragma mark -- 判斷是否iPhone4,4s,5,6,plus,iPad//不會隨著應用內部橫屏和豎屏變化#define DeviceCurrentModeSize [UIScreen mainScreen].currentMode.size#define DevicePortraitModeSize (DeviceCurrentModeSize.width < DeviceCurrentModeSize.height ? DeviceCurrentModeSize : CGSizeMake(DeviceCurrentModeSize.height, DeviceCurrentModeSize.width))#define DeviceIsNotRetina CGSizeEqualToSize(DevicePortraitModeSize, CGSizeMake(320, 480))#define DeviceIsiPhone4s CGSizeEqualToSize(DevicePortraitModeSize, CGSizeMake(640, 960))#define DeviceIsiPhone5 CGSizeEqualToSize(DevicePortraitModeSize, CGSizeMake(640, 1136))//6s與6同尺寸#define DeviceIsiPhone6 CGSizeEqualToSize(DevicePortraitModeSize, CGSizeMake(750, 1334))//6s Plus 與 6Plus同尺寸(部分機型實際上只有6s的bounds.size)#define DeviceIsiPhone6plus (CGSizeEqualToSize(DevicePortraitModeSize, CGSizeMake(1125, 2001)) || CGSizeEqualToSize(DevicePortraitModeSize, CGSizeMake(1242, 2208)))//在模擬器上調試,可能會遇到非Retina解析度的情況#define DeviceIsiPad (CGSizeEqualToSize(DevicePortraitModeSize, CGSizeMake(768, 1024)) || CGSizeEqualToSize(DevicePortraitModeSize, CGSizeMake(1536, 2048)) || CGSizeEqualToSize(DevicePortraitModeSize, CGSizeMake(1024, 1366)) || CGSizeEqualToSize(DevicePortraitModeSize, CGSizeMake(2048, 2732)))#pragma mark -- 判斷裝置作業系統整數版本號碼//當前系統版本號碼取得仍為浮點型#define DeviceIOSVersion [[[UIDevice currentDevice] systemVersion] floatValue]#define DeviceIOSVersionIs(x) (DeviceIOSVersion >= x && DeviceIOSVersion < x+1 ? YES : NO)#define DeviceIOSVersionAbove(x) (DeviceIOSVersion >= x ? YES : NO)#pragma mark -- 根據裝置,計算標註圖中當前縮放尺寸(標註圖版本:375寬度) iPad以iPhone6為標準顯示//下列宏定義,只作為一種裝置適配參考方案#define ResizeSideBase6(s) (DeviceIsiPad ? s : (DeviceWidth * s / 375))#pragma mark -- 只根據設計圖(標註圖版本:375寬度)的寬度等比縮放#define ResizeSideBase375(s) (DeviceWidth * s / 375)#pragma mark -- 設定自訂字型#define FontName1 @"FZY3JW--GB1-0"//方正准圓ttf字型名稱#define FontName2 @"Helvetica"#define FontName3 @"Helvetica-Bold"//固定字型大小#define FONTAppliedFixed(n) [UIFont systemFontOfSize:n]#define FONTAppliedBoldFixed(n) [UIFont boldSystemFontOfSize:n]#define FONTFZZYFixed(n) [UIFont fontWithName:FontName1 size:n]#define FONTHelveticaFixed(n) [UIFont fontWithName:FontName2 size:n]#define FONTHelveticaBoldFixed(n) [UIFont fontWithName:FontName3 size:n]//下列宏定義,只作為一種裝置適配參考方案//根據裝置,計算顯示字型(標註圖版本:375寬度) iPad以iPhone6為標準顯示#define FONTAppliedBase6(n) (DeviceIsiPhone6plus ? FONTAppliedFixed(n+0.5) : (DeviceIsiPhone6 || DeviceIsiPad ? FONTAppliedFixed(n) : FONTAppliedFixed(n-1)))#define FONTAppliedBoldBase6(n) (DeviceIsiPhone6plus ? FONTAppliedBoldFixed(n+0.5) : (DeviceIsiPhone6 || DeviceIsiPad ? FONTAppliedBoldFixed(n) : FONTAppliedBoldFixed(n-1)))#define FONTHelveticaBase6(n) (DeviceIsiPhone6plus ? FONTHelveticaFixed(n+0.5) : (DeviceIsiPhone6 || DeviceIsiPad ? FONTHelveticaFixed(n) : FONTHelveticaFixed(n-1)))#define FONTHelveticaBoldBase6(n) (DeviceIsiPhone6plus ? FONTHelveticaBoldFixed(n+0.5) : (DeviceIsiPhone6 || DeviceIsiPad ? FONTHelveticaBoldFixed(n) : FONTHelveticaBoldFixed(n-1)))#pragma mark -- 載入圖片宏(下列方法頻繁IO,不緩衝圖片):#define LOADIMAGE(name) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:name ofType:nil]]#define LOADIMAGEWITHTYPE(name,type) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:name ofType:type]]#pragma mark -- 設定16進位RGB顏色(格式:RRGGBB)#define COLORWITHRRGGBBA(RRGGBB, A) [UIColor colorWithRed:((float)((RRGGBB & 0xFF0000) >> 16))/255.0 green:((float)((RRGGBB & 0xFF00) >> 8))/255.0 blue:((float)(RRGGBB & 0xFF))/255.0 alpha:A]#define COLORWITHRRGGBB(RRGGBB) COLORWITHRRGGBBA(RRGGBB, 1.0)#pragma mark -- 設定10進位RGB顏色#define COLORWITHRGBA(R, G, B, A) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A]#define COLOR(R, G, B) COLORWITHRGBA(R, G, B, 1.0)#define COLORWITHIMAGE(name) [UIColor colorWithPatternImage:LOADIMAGE(name)]#pragma mark -- 角度弧度轉換#define DegreesToRadian(x) (M_PI * x / 180.0)#define RadianToDegrees(radian) (radian * 180.0 / M_PI)#pragma mark -- log輸出控制宏#ifdef DEBUG#define LOG(...) NSLog(__VA_ARGS__);#define LOG_METHOD NSLog(@"%s", __func__);#else#define LOG(...) ;#define LOG_METHOD ;#endif#pragma mark -- 弱引用宏定義#define WS(weakSelf) __weak typeof(self) weakSelf = self;
說明如下:
1.[UIScreen mainScreen].bounds.size會隨著橫屏和豎屏變化,[[UIScreen mainScreen] currentMode].size不會隨之改變。
例如,將工程General中橫屏勾選上,在iPhone6s上運行,一開始為豎屏(375,667),應用旋轉到橫屏後,前者寬高對調(667,375);後者始終為(750,1334)
2.DeviceIsiPhone6plus 判斷是否為6Plus時候,遇到過一些解析度實際為(1125, 2001)的測試機,也就是說bounds.size為(375,667),猜測是翻新機
3.DeviceIsiPad判斷iPad時候,多了非Retina解析度的比對。因為在iPad上調試僅為iPhone類型的應用,會遇到此類型的解析度
4.ResizeSide和FONT系列的宏定義,提供了寬度和字型用代碼適配多裝置的簡單參考方案,當然Masonry類庫是代碼適配裝置最好的方案,這裡只是用於便捷使用
5.LOADIMAGE宏定義,採用了頻繁IO的方式來載入圖片資源,目的是避免記憶體的緩衝長時間佔用。建議需要保留對象時候,增加屬性或者變數引用即可
6.若引用宏定義中,__weak typeof(self) weakSelf = self;寫法中的typeof也可以替換為舊版__typeof,不過建議使用新的寫法,即typeof
著重記錄一下自訂字型的使用
之前在配置Info.plist檔案時候,添加了字型的索引值對,現在就可以派上用途了。
1.先將.ttf字型添加到項目中,然後在Info.plist檔案的Source code中,在UIAppFonts鍵下增加如下代碼:
<key>UIAppFonts</key> <array> <string>fzzy.ttf</string> </array>
當然,也可以在列表模式下,找到Fonts provided by application鍵,然後添加一個item,將ttf檔案的名稱填入其中。
2.在合適的地方(例如AppDelegate的應用啟動後的代理方法中)使用如下代碼:
for (NSString* familyName in [UIFont familyNames]) { LOG(@"familyName : %@", familyName); for (NSString* fontName in [UIFont fontNamesForFamilyName:familyName]) { LOG(@"fontName : %@", fontName); } }
3.運行應用後,在控制台查看字型輸出log,既然用的是fzzy字型,所以實際名稱應該類似。所以找到了“FZY3JW--GB1-0”這樣的字型名稱
4.可以查看FONTFZZYFixed宏定義的字型顯示效果
需要注意的是,同一字型族中,細體、粗體、斜體等,都是不同的字型名稱,可以理解為在使用不同的字型。一個ttf字型檔,一般只有一種字型,具體情況需要查看控制台輸出的字型log。
標頭檔的使用
在Supporting Files檔案目錄下建立標頭檔:
我將通用宏定義放入了CommonHeader.h,便於統一管理以及擴充。
類似的,不同類型宏定義,建議組織為不同標頭檔,例如:StringsHeader.h,ImagesHeader.h,URLsHeader.h。
最後,將所有標頭檔統的引用統一加入到先行編譯標頭檔中PrefixHeader.h全域生效。
base項目已更新:git@github.com:ALongWay/base.git