You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
234 lines
8.1 KiB
234 lines
8.1 KiB
1 year ago
|
//
|
||
|
// Const.swift
|
||
|
// VeSync
|
||
|
//
|
||
|
// Created by Sheldon on 2019/8/7.
|
||
|
// Copyright © 2019 Etekcity. All rights reserved.
|
||
|
//
|
||
|
|
||
|
import Foundation
|
||
|
import UIKit
|
||
|
|
||
|
// MARK: - Siwft全局第三方常用库引用
|
||
|
@_exported import RxSwift
|
||
|
@_exported import RxCocoa
|
||
|
@_exported import RxDataSources
|
||
|
@_exported import ReusableKit
|
||
|
@_exported import Then
|
||
|
@_exported import SnapKit
|
||
|
@_exported import HandyJSON
|
||
|
@_exported import SwiftyJSON
|
||
|
@_exported import NSObject_Rx
|
||
|
@_exported import SVProgressHUD
|
||
|
@_exported import NVActivityIndicatorView
|
||
|
|
||
|
// MARK: - 常用尺寸
|
||
|
let kScreenBounds = UIScreen.main.bounds
|
||
|
let kScreenScale = UIScreen.main.scale
|
||
|
let kScreenSize = kScreenBounds.size
|
||
|
let kScreenW = kScreenSize.width
|
||
|
let kScreenH = kScreenSize.height
|
||
|
let kNavBarItemMargin: CGFloat = 20.0
|
||
|
|
||
|
// MARK: - 常用颜色
|
||
|
let kMainLgihtGray = kHexColor(0xf7f7f7)
|
||
|
let kMainGreen = kHexColor(0x0da778)
|
||
|
let kBackgroundColor = kHexColor(0xffffff)
|
||
|
|
||
|
// MARK: - 常用全局单例
|
||
|
let kUserDefualt = UserDefaults.standard
|
||
|
let kNotificationCenter = NotificationCenter.default
|
||
|
let keyWindow = UIApplication.shared.keyWindow!
|
||
|
|
||
|
// MARK: - 常用闭包
|
||
|
typealias ActionClosure = () -> Void
|
||
|
|
||
|
// MARK: - APP&设备信息
|
||
|
let kAppInfoDict = Bundle.main.infoDictionary
|
||
|
let kAppCurrentVersion = kAppInfoDict!["CFBundleShortVersionString"]
|
||
|
let kAppBuildVersion = kAppInfoDict!["CFBundleVersion"]
|
||
|
let kDeviceIosVersion = UIDevice.current.systemVersion
|
||
|
let kDeviceIdentifierNumber = UIDevice.current.identifierForVendor
|
||
|
let kDeviceSystemName = UIDevice.current.systemName
|
||
|
let kDeviceModel = UIDevice.current.model
|
||
|
let kDeviceLocalizedModel = UIDevice.current.localizedModel
|
||
|
let isIPhone = (UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.phone ? true : false)
|
||
|
let isIPad = (UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad ? true : false)
|
||
|
let isIPhone4 = (max(kScreenW, kScreenH) < 568.0 ? true : false)
|
||
|
let isIPhone5 = (max(kScreenW, kScreenH) == 568.0 ? true : false)
|
||
|
let isIPhone6 = (max(kScreenW, kScreenH) == 667.0 ? true : false)
|
||
|
let isIPhone6P = (max(kScreenW, kScreenH) == 736.0 ? true : false)
|
||
|
let isIPhoneX = ((kScreenH == 812.0 || kScreenH == 896.0) ? true : false)
|
||
|
let kNavibarH: CGFloat = isIPhoneX ? 88.0 : 64.0
|
||
|
let kTabbarH: CGFloat = isIPhoneX ? 49.0 + 34.0 : 49.0
|
||
|
let kBatteryH: CGFloat = isIPhoneX ? 24.0 : 20.0
|
||
|
let kStatusbarH: CGFloat = isIPhoneX ? 44.0 : 20.0
|
||
|
let iPhoneXBottomH: CGFloat = 34.0
|
||
|
let iPhoneXTopH: CGFloat = 24.0
|
||
|
let kIOS8 = (kDeviceIosVersion as NSString).doubleValue >= 8.0
|
||
|
let kIOS9 = (kDeviceIosVersion as NSString).doubleValue >= 9.0
|
||
|
let kIOS10 = (kDeviceIosVersion as NSString).doubleValue >= 10.0
|
||
|
let kIOS11 = (kDeviceIosVersion as NSString).doubleValue >= 11.0
|
||
|
|
||
|
// MARK: - 常用字体
|
||
|
func SystemRegularFont(_ size: CGFloat, isScale: Bool = true) -> UIFont {
|
||
|
let fontSize = isScale ? kScaleWidth(size) : size
|
||
|
let font = UIFont.init(name: "PingFangSC-Regular", size: fontSize)
|
||
|
return font!
|
||
|
}
|
||
|
|
||
|
func SystemMediumFont(_ size: CGFloat, isScale: Bool = true) -> UIFont {
|
||
|
let fontSize = isScale ? kScaleWidth(size) : size
|
||
|
let font = UIFont.init(name: "PingFangSC-Medium", size: fontSize)
|
||
|
return font!
|
||
|
}
|
||
|
|
||
|
func SystemSemiboldFont(_ size: CGFloat, isScale: Bool = true) -> UIFont {
|
||
|
let fontSize = isScale ? kScaleWidth(size) : size
|
||
|
let font = UIFont.init(name: "PingFangSC-Semibold", size: fontSize)
|
||
|
return font!
|
||
|
}
|
||
|
|
||
|
func SystemLightFont(_ size: CGFloat, isScale: Bool = true) -> UIFont {
|
||
|
let fontSize = isScale ? kScaleWidth(size) : size
|
||
|
let font = UIFont.init(name: "PingFangSC-Light", size: fontSize)
|
||
|
return font!
|
||
|
}
|
||
|
|
||
|
func BebasFont(_ size: CGFloat, isScale: Bool = true) -> UIFont {
|
||
|
let fontSize = isScale ? kScaleWidth(size) : size
|
||
|
let font = UIFont.init(name: "BEBAS_s", size: fontSize)
|
||
|
return font!
|
||
|
}
|
||
|
|
||
|
/// 常用数字字体
|
||
|
func DigitalFont(_ size: CGFloat) -> UIFont {
|
||
|
let font = UIFont.init(name: "DIN Condensed", size: kScaleWidth(size))
|
||
|
return font!
|
||
|
}
|
||
|
|
||
|
// MARK: - 等比例适配宏
|
||
|
func kScaleWidth(_ width: CGFloat) -> CGFloat {
|
||
|
return width * kScreenW / 375
|
||
|
}
|
||
|
func kScaleHeight(_ height: CGFloat) -> CGFloat {
|
||
|
return isIPhoneX ? height: height * kScreenH / 667
|
||
|
}
|
||
|
|
||
|
/// MARK:- 获取当前最上层控制器
|
||
|
func currentViewController() -> UIViewController? {
|
||
|
guard let vc = UIApplication.shared.keyWindow?.rootViewController else{
|
||
|
return nil
|
||
|
}
|
||
|
if vc.isKind(of: UINavigationController.self){
|
||
|
guard let vc = (vc as! UINavigationController).visibleViewController else{
|
||
|
return nil
|
||
|
}
|
||
|
return vc
|
||
|
}else if vc.isKind(of: UITabBarController.self){
|
||
|
guard let vc = (vc as! UITabBarController).selectedViewController else{
|
||
|
return nil
|
||
|
}
|
||
|
if vc.isKind(of: UINavigationController.self) {
|
||
|
guard let vc = (vc as! UINavigationController).visibleViewController else{
|
||
|
return nil
|
||
|
}
|
||
|
return vc
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
func currentNavtigationController() -> UINavigationController? {
|
||
|
return currentViewController()?.navigationController
|
||
|
}
|
||
|
func currentTabbarController() -> UITabBarController? {
|
||
|
return currentViewController()?.tabBarController
|
||
|
}
|
||
|
|
||
|
// MARK: - 颜色方法
|
||
|
func kRGBA(_ r: CGFloat, _ g: CGFloat, _ b: CGFloat, _ a: CGFloat) -> UIColor {
|
||
|
|
||
|
return UIColor (red: r/255.0, green: g/255.0, blue: b/255.0, alpha: a)
|
||
|
}
|
||
|
|
||
|
func kHexColor(_ hex: Int64) -> UIColor {
|
||
|
return UIColor.hexColor(hex)
|
||
|
}
|
||
|
|
||
|
func kHexColor(_ hex: Int64, _ alpha: CGFloat) -> UIColor {
|
||
|
return UIColor.hexColor(hex, alpha)
|
||
|
}
|
||
|
|
||
|
func kRandomColor() -> UIColor {
|
||
|
return kRGBA(CGFloat(arc4random_uniform(256)), CGFloat(arc4random_uniform(256)), CGFloat(arc4random_uniform(256)), 1)
|
||
|
}
|
||
|
|
||
|
// MARK:- 自定义内容消息提示框单个
|
||
|
func showAlert(_ viewController: UIViewController, _ contextText: String, confirmText:String, handle: @escaping () -> ()) {
|
||
|
let alertController = UIAlertController(title: contextText, message: nil, preferredStyle: .alert)
|
||
|
let confirmAction = UIAlertAction(title: confirmText, style: .default) { _ in
|
||
|
handle()
|
||
|
}
|
||
|
alertController.addAction(confirmAction)
|
||
|
viewController.present(alertController, animated: true, completion: nil)
|
||
|
}
|
||
|
|
||
|
// MARK:- 自定义内容消息提示框
|
||
|
func showAlert(_ viewController: UIViewController, _ contextText: String, cancelText: String, confirmText:String, handle: @escaping (Bool) -> ()) {
|
||
|
let alertController = UIAlertController(title: contextText, message: nil, preferredStyle: .alert)
|
||
|
let cancelAction = UIAlertAction(title: cancelText, style: .cancel) { _ in
|
||
|
handle(false)
|
||
|
}
|
||
|
let confirmAction = UIAlertAction(title: confirmText, style: .default) { _ in
|
||
|
handle(true)
|
||
|
}
|
||
|
alertController.addAction(cancelAction)
|
||
|
alertController.addAction(confirmAction)
|
||
|
viewController.present(alertController, animated: true, completion: nil)
|
||
|
}
|
||
|
|
||
|
// MARK: - 打印方法
|
||
|
func LFLogs<T>(_ message : T, file : String = #file, funcName : String = #function, lineNum : Int = #line) {
|
||
|
#if DEBUG
|
||
|
let fileName = (file as NSString).lastPathComponent
|
||
|
print("\(DateClass.todayIntegrateString()):\(fileName):[\(lineNum)]:\(message)")
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
func logDebugs(_ format: String, _ logins: CVarArg..., file: String = #file, funcName: String = #function, lineNum: Int = #line) {
|
||
|
#if DEBUG
|
||
|
let file = (file as NSString).lastPathComponent
|
||
|
let str = String(format: "\(file):\(funcName):(\(lineNum))--\(format)", logins)
|
||
|
NSLog(str)
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
/// RxSwift 资源打印
|
||
|
func logResourcesCountss(file: String = #file, funcName: String = #function, lineNum: Int = #line) {
|
||
|
#if DEBUG
|
||
|
let file = (file as NSString).lastPathComponent
|
||
|
let format = "RxSwift resources count: \(RxSwift.Resources.total)"
|
||
|
let str = "\(file):\(funcName):(\(lineNum))--\(format)"
|
||
|
NSLog(str)
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
/// 定时打印资源,调试用
|
||
|
func rxswiftResourcesTotalDebugLogs() {
|
||
|
#if DEBUG
|
||
|
_ = Observable<Int>.interval(.seconds(10), scheduler: MainScheduler.instance)
|
||
|
.subscribe(onNext: { _ in
|
||
|
print("Rx Resource Count: \(RxSwift.Resources.total)")
|
||
|
})
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
// MARK:- 国际化字符串
|
||
|
|
||
|
extension String {
|
||
|
var localized: String {
|
||
|
return Bundle.main.localizedString(forKey: self, value: "", table: nil)
|
||
|
}
|
||
|
}
|