OC版本
//获取屏幕的宽、高
#define KScreenWidth [UIScreen mainScreen].bounds.size.width
#define KScreenHeight [UIScreen mainScreen].bounds.size.height
// __block
#define kBlcokSelf(pSelf) __block typeof(&*self) pBlcokSelf = self
//状态栏的高度
#ifdef __IPHONE_13_0 //判断是否是13以上系统
#define statusBarHeight [[UIApplication sharedApplication] windows].firstObject.windowScene.statusBarManager.statusBarFrame.size.height
#else
#define statusBarHeight [[UIApplication sharedApplication] statusBarFrame].size.height
#endif
//状态栏+导航栏高度
#define NavAndstatusBarHeight (statusBarHeight+44)
//判断是否是刘海屏
#define iPhoneX ((statusBarHeight != 20) ? YES : NO)
//底部安全距离
#define bottomLayoutIPhoneX (iPhoneX ? 34 : 0)
//判断手机屏幕尺寸
//3.5寸屏幕
#define ThreePointFiveInch ([UIScreen mainScreen].bounds.size.height == 480.0)
//4.0寸屏幕(iPhone SE)
#define FourInch ([UIScreen mainScreen].bounds.size.height == 568.0)
//4.7寸屏幕(iPhone 6/6s/7/8/SE2)
#define FourPointSevenInch ([UIScreen mainScreen].bounds.size.height == 667.0)
//5.4寸屏幕(iPhone 12 mini/13 mini)
#define FivePointFourSevenInch ([UIScreen mainScreen].bounds.size.height == 780.0)
//5.5寸屏幕(iPhone 6p/7p/8p)
#define FivePointFiveSevenInch ([UIScreen mainScreen].bounds.size.height == 736.0)
//5.8寸屏幕(iPhone x/xs/11Pro)
#define FivePointEightSevenInch ([UIScreen mainScreen].bounds.size.height == 812.0)
//6.1(iPhone xr/11/12/12 Pro/13/13 Pro/14/14Pro)
#define SixPointOneSevenInch (([UIScreen mainScreen].bounds.size.height == 896.0 && [UIScreen mainScreen].scale == 2) || [UIScreen mainScreen].bounds.size.height == 844.0 || [UIScreen mainScreen].bounds.size.height == 852.0)
//6.5寸屏幕(iPhone xsMax/11ProMax)
#define SixPointFiveSevenInch ([UIScreen mainScreen].bounds.size.height == 896.0 && [UIScreen mainScreen].scale == 3)
//6.7寸屏幕(iPhone 12 Pro Max/13 Pro Max/14Plus/14ProMax)
#define SixPointSevenSevenInch ([UIScreen mainScreen].bounds.size.height == 926.0 || [UIScreen mainScreen].bounds.size.height == 932)
//判断是否从右往左读取(RTL,阿拉伯语是RTL)这里只适配了阿拉伯语
#define isRTL [[NSLocale preferredLanguages].firstObject hasPrefix:@"ar-"]
//设置颜色
#define HexRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
#define HexRGBAlpha(rgbValue,a) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:(a)]
//GCD (子线程、主线程定义)
#define BACK(block) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block)
#define MAIN(block) dispatch_async(dispatch_get_main_queue(),block)
// 获取版本号
#define LOCAL_RELEASE_VERSION [[NSBundle mainBundle]infoDictionary][@"CFBundleShortVersionString"]
// 获取手机型号
#define Phone_Model [[UIDevice currentDevice] model]
// 系统版本号
#define System_Version [[UIDevice currentDevice] systemVersion]
//设置本地图片
#define TL_IMAGE(name) [UIImage imageNamed:name]
Swift版本
//获取屏幕高宽
let ScreenWith = UIScreen.main.bounds.size.width
let ScreenHeight = UIScreen.main.bounds.size.height
//返回状态栏高度
#if __IPHONE_13_0
let statusBarHeight = UIApplication.shared.windows.first?.windowScene?.statusBarManager?.statusBarFrame.size.height
#else
let statusBarHeight = UIApplication.shared.statusBarFrame.size.height
#endif
//返回状态栏和导航栏的高度
let NavAndstatusBarHeight = statusBarHeight + 44
//判断是否是刘海屏
let iphoneX = ((statusBarHeight != 20) ? true : false)
// 获取版本号
let LOCAL_RELEASE_VERSION = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
// 获取手机型号
let Phone_Model = UIDevice.current.model
// 系统版本号
let System_Version = UIDevice.current.systemVersion
//获取当前语言
let Language_first_local = NSLocale.preferredLanguages.first!
//GCD(进入异步线程)
func GCDThread (completion: (() -> Void)? = nil) {
DispatchQueue.global(qos: .default).async {
completion!()
}
}
//回到主线程
func MainThread (completion: (() -> Void)? = nil) {
DispatchQueue.main.async {
completion!()
}
}
//延迟执行
func AfterGCD (timeInval:CGFloat, completion: (() -> Void)? = nil) {
DispatchQueue.main.asyncAfter(deadline: .now() + timeInval) {
completion!()
}
}
// MARK: - 颜色
var RGBA:(NSInteger, NSInteger, NSInteger, CGFloat) -> UIColor = { (r,g,b,a) in
return UIColor(red: CGFloat(r)/255.0, green: CGFloat(g)/255.0, blue: CGFloat(b)/255.0, alpha: a)
}
var RGB:(NSInteger, NSInteger, NSInteger) -> UIColor = { (r,g,b) in
return UIColor(red: CGFloat(r)/255.0, green: CGFloat(g)/255.0, blue: CGFloat(b)/255.0, alpha: 1.0)
}
//随机颜色
let RANDOM_COLOR = RGB(Int(arc4random())%255, Int(arc4random())%255, Int(arc4random())%255)
//16进制颜色转换
var HexRGB:(NSInteger) -> UIColor = { hex in
return UIColor(red: ((CGFloat)((hex & 0xFF0000) >> 16)) / 255.0, green: ((CGFloat)((hex & 0xFF00) >> 8)) / 255.0, blue: ((CGFloat)((hex & 0xFF))) / 255.0, alpha: 1.0)
}
//16进制颜色转换(带透明度)
var HexRGBAlpha:(NSInteger, CGFloat) -> UIColor = { (hex, alpha) in
return UIColor(red: ((CGFloat)((hex & 0xFF0000) >> 16)) / 255.0, green: ((CGFloat)((hex & 0xFF00) >> 8)) / 255.0, blue: ((CGFloat)((hex & 0xFF))) / 255.0, alpha: alpha)
}
//storyboard
var Storyboard_INSTANT_VC_WITH_ID:(String, String) -> UIViewController = { name,identifier in
return UIStoryboard.init(name: name, bundle: nil).instantiateViewController(withIdentifier: identifier)
}
//nib
var Nib_INSTANT_WITH_NAME:(String) -> Any? = { name in
return Bundle.main.loadNibNamed(name, owner: nil, options: nil)?.first
}
//设置本地图片
var imageName:(String) -> UIImage = { imgName in
return UIImage(named: imgName)!
}
/// 判断设备是 iPhone
let isIPhone = UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.phone
/// 判断设备是 iPad
let isIPad = UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad
/// 判断是否是横屏
public func isIsLandscape() -> Bool {
return UIDevice.current.orientation.isLandscape || UIApplication.shared.statusBarOrientation == UIInterfaceOrientation.landscapeLeft || UIApplication.shared.statusBarOrientation == UIInterfaceOrientation.landscapeRight
}
///获取keywindow
#if __IPHONE_13_0
let KeyWindows = UIApplication.shared.windows.first
#else
let KeyWindows = UIApplication.shared.keyWindow
#endif
///获取Document路径
let DocumentPath_Local = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! + "/"
///获取caches路径
let CachesPath_Local = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first! + "/"
///获取caches路径
let LibraryPath_Local = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true).first! + "/"
///获取temp路径
let TempPath_Local = NSTemporaryDirectory()
///打印log
func printLog(_ items: Any..., separator: String = " ", terminator: String = "\n") {
#if DEBUG
let currentDate = Date()
let calendar = Calendar.current
let components = calendar.dateComponents([.nanosecond], from: currentDate)
let nanosecond = components.nanosecond ?? 000
// 由于 DateComponents 的 nanosecond 是以纳秒为单位,我们需要将其转换为毫秒
let milliseconds = nanosecond/1000000
let paddedNumber = String(format: "%03d", milliseconds)
let dateformatter = DateFormatter()
dateformatter.dateFormat = "YYYY-MM-dd HH:mm:ss"// 自定义时间格式
let time = dateformatter.string(from: currentDate)
print("\(time) \(paddedNumber):", terminator:"")
print(items, separator: separator, terminator: terminator)
// print("当前是Debug模式")
#else
// print("当前不是Debug模式")
#endif
}
/// 添加通知
/// - Parameters:
/// - name: 通知名称
/// - object: 传递的参数
func NotificationCenterPost(name:String, object:Any? = nil) {
NotificationCenter.default.post(name: NSNotification.Name(name), object: object)
}
/// 接收通知
/// - Parameters:
/// - name: 通知名称
/// - isMainThread: 是否在主线程
/// - completion: 通知执行的方法
func NotificationCenterAdd(name:String, isMainThread:Bool = true, completion:@escaping (Notification) -> Void) {
NotificationCenter.default.addObserver(forName: NSNotification.Name(name), object: nil, queue: (isMainThread ? .main : .current)) { not in
completion(not)
}
}