iOS開發(fā)常用的宏定義

Objective-C常用宏
/*! 字體 */

/*! 常用字體 */
#define FONT(a)          [UIFont systemFontOfSize:a]
/*! 字體(粗) */
#define BoldFONT(a)      [UIFont boldSystemFontOfSize:a]

/*! 顏色宏定義 */

/*! RGB顏色 */
#define RGBA(r, g, b, a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]
#define RGB(r,g,b)       [UIColor colorWithRed:r/255.f green:g/255.f blue:b/255.f alpha:1.0]
/**! 16進(jìn)制顏色 */
#define RGBFrom(rgbValue) \
[UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >>16))/255.0 \
green:((float)((rgbValue & 0x00FF00) >>  8))/255.0 \
blue:((float)((rgbValue & 0x0000FF) >>  0))/255.0 \
alpha:1.0]

/*! 弱引用宏 */

#define WeakObj(o)       try{}@finally{} __weak typeof(o) o##Weak = o;

/*! 輸出顯示所在類,所在行,方法名,內(nèi)容,DEBUG時輸出,Release時不輸出 */

#ifdef DEBUG
#define NSLog(format, ...) do { \
fprintf(stderr,"\n《+++++++++++++++++++ START +++++++++++++++++++++》\n \n《==所在類==》:%s \n《==所在行==》:%d \n《==方法名==》:%s\n\n", \
[[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], \
__LINE__, __func__); \
(NSLog)((format), ##__VA_ARGS__); \
fprintf(stderr,"\n《-------------------  END  ---------------------》\n \n"); \
} while (0)
#else
#define NSLog(...)
#define debugMethod()
#endif

/*! 常用系統(tǒng)信息宏 */

#define ScreenWidth  [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
#define  iphone4_5   ([[UIScreen mainScreen] bounds].size.width ==320)
#define  iPhone4     ([[UIScreen mainScreen] bounds].size.height==480)
#define  iPhone5     ([[UIScreen mainScreen] bounds].size.height==568)
#define  iPhone6     ([[UIScreen mainScreen] bounds].size.height==667)
#define  iPhone6plus ([[UIScreen mainScreen] bounds].size.height==736)
#define  iPad        ([[UIScreen mainScreen] bounds].size.height>736)
#define SystemVersion [[UIDevice  currentDevice] systemVersion] //設(shè)備版本
#define SystemModel   [[UIDevice currentDevice] model] //設(shè)備模式
#define BundleVersion [[[NSBundle mainBundle]infoDictionary] objectForKey:@"CFBundleShortVersionString"] //當(dāng)前app版本

Swift常用宏
swift中是不能使用宏定義語法的,但是因?yàn)槊臻g的緣故,我們可以給我們的項(xiàng)目添加一個空的.swift文件,在其中,我們將原本oc中不需要接受參數(shù)的宏,定義成let常量,將需要接受參數(shù)的宏定義成函數(shù)或var即可,由于我們的整個項(xiàng)目共享命名空間,我們就可以在項(xiàng)目內(nèi)的任何地方直接使用.swift文件中定義的這些公共的常量和函數(shù)
/*! 字體 */

/// 系統(tǒng)普通字體
var WG_SystemFontWithSize: (CGFloat) -> UIFont = {size in
    return UIFont.systemFont(ofSize: size);
}
/// 系統(tǒng)加粗字體
var WG_BoldFontWithSize: (CGFloat) -> UIFont = {size in
     return UIFont.boldSystemFont(ofSize: size);
}

/*! 顏色 */

/// 根據(jù)RGBA生成顏色(格式為:22,22,22,0.5)
var WG_RGBAColor: (CGFloat, CGFloat, CGFloat, CGFloat) ->UIColor = {red, green, blue, alpha in
     return UIColor(red: red / 255, green: green / 255, blue: blue / 255, alpha: alpha);
}
/// 根據(jù)RGB生成顏色(格式為:22,22,22)
var WG_RGBColor: (CGFloat, CGFloat, CGFloat) -> UIColor = {red, green, blue in
      return UIColor(red: red / 255, green: green / 255, blue: blue / 255, alpha: 1);
}
///// 根據(jù)色值生成顏色(無透明度)(格式為0xffffff)
func RGBColorFromHex(rgbValue: Int) -> (UIColor) {
       return UIColor(red: ((CGFloat)((rgbValue & 0xFF0000) >> 16)) / 255.0,green: ((CGFloat)((rgbValue & 0xFF00) >> 8)) / 255.0,blue: ((CGFloat)(rgbValue & 0xFF)) / 255.0, alpha: 1.0)
}

// MARK:- 自定義打印方法

func WGLog<T>(_ message : T, file : String = #file, funcName : String = #function, lineNum : Int = #line) {
#if DEBUG
    let fileName = (file as NSString).lastPathComponent
    print("所在類:\(fileName) \n所在行:\(lineNum) \n信 息:\(message) \n<><><><><>-「END」-<><><><><>\n")
#endif
}

// MARK: - 系統(tǒng)版本

/// 獲取系統(tǒng)版本號
let kSystemVersion = Float(UIDevice.current.systemVersion);
/// 是否IOS8系統(tǒng)
let kIsIOS8OrLater = Int(UIDevice.current.systemVersion)! >= 8 ? true : false;
/// 是否IOS9系統(tǒng)
let kIsIOS9OrLater = Int(UIDevice.current.systemVersion)! >= 9 ? true : false;

// MARK: - 常用寬高

/// 屏幕Bounds
let kScreenBounds = UIScreen.main.bounds;
/// 屏幕高度
let kScreenHeight = UIScreen.main.bounds.size.height;
/// 屏幕寬度
let kScreenWidth = UIScreen.main.bounds.size.width;
/// 導(dǎo)航欄高度
let kNavBarHeight = 44.0;
/// 狀態(tài)欄高度
let kStatusBarHeight = 20.0;
/// Tab欄高度
let kTabBarHeight = 49.0;

//根據(jù)圖片名稱獲取圖片

let WG_ImageWithName: (String) -> UIImage? = {imageName in
     return UIImage(named: imageName);
}

以后用到再添加....

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 在這里給大家分享一些常用的宏定義,喜歡的小伙伴可以直接在項(xiàng)目中使用。 目錄 1.獲取屏幕寬度與高度2.獲取通知中心...
    CN_HarrySun閱讀 441評論 3 9
  • 大家都是知道開發(fā)中使用宏不僅方便,而且可以提高開發(fā)效率, 代碼清晰易懂。下面我總結(jié)了我在做iOS開發(fā)時的一些常用宏...
    AbnerZhang閱讀 382評論 0 2
  • 一個好的iOS開發(fā)工程師必須學(xué)會使用宏定義,不僅可以提高開發(fā)效率,而且高端、大氣、上檔次。下面是我總結(jié)的一些常用的...
    my_楊哥閱讀 1,125評論 6 20
  • 一、上周重點(diǎn)工作完成情況 1.根據(jù)之前確定的旅游板塊內(nèi)容方向,周一至周三細(xì)化了旅游頻道的改版方案; 選稿原則、內(nèi)容...
    wowShinyrose閱讀 157評論 0 0
  • 近日,演員秦海璐被拍到在地下車庫做出了很多怪異的行為。當(dāng)天,秦海璐披頭散發(fā),雙手握拳對著空氣一頓狂砸,聲嘶力竭、歇...
    約能人閱讀 1,061評論 0 0