Const.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // Const.swift
  3. // swift翻译
  4. //
  5. // Created by zhongyuan on 2018/3/20.
  6. // Copyright © 2018年 zhongyuan. All rights reserved.
  7. //
  8. import UIKit
  9. let kScreenWidth = UIScreen.main.bounds.size.width
  10. let kScreenHeight = UIScreen.main.bounds.size.height
  11. let Is_Iphone = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.phone)
  12. let Is_Iphone_XxS = (Is_Iphone && kScreenWidth == 375.0 && kScreenHeight == 812.0)
  13. let Is_Ipone_XRxSMax = (Is_Iphone && kScreenWidth == 414.0 && kScreenHeight == 896.0)
  14. let Is_Ipone12_ProMax = (Is_Iphone && kScreenWidth == 428.0 && kScreenHeight == 926.0)
  15. let Is_Iphone_6p7p8p = (Is_Iphone && kScreenWidth == 414.0 && kScreenHeight == 736.0)
  16. let Is_Iphone_678 = (Is_Iphone && kScreenWidth == 375.0 && kScreenHeight == 667.0)
  17. let Is_Iphone_5 = (Is_Iphone && kScreenWidth == 320.0 && kScreenHeight == 568.0)
  18. let Is_Iphone_4 = (Is_Iphone && kScreenWidth == 320.0 && kScreenHeight == 480.0)
  19. let hasSafeArea = Is_Ipone12_ProMax || Is_Iphone_XxS || Is_Ipone_XRxSMax
  20. let NaviHeight = (CGFloat)(hasSafeArea ? 88 : 64)
  21. let TabbarHeight = (CGFloat)(hasSafeArea ? 83 : 49)
  22. /// 底部安全区域远离高度
  23. let kBottomSafeHeight = (CGFloat)(hasSafeArea ? 34 : 0)
  24. /// 顶部安全区域远离高度
  25. let kTopBarSafeHeight = (CGFloat)(hasSafeArea ? 44:0)
  26. /// 状态栏高度
  27. let kStatusBarHeight = (CGFloat)(hasSafeArea ? 44:20)
  28. /// 获取应用名称和版本号
  29. let app_Name = (Bundle.main.infoDictionary! as NSDictionary)
  30. .object(forKey: "CFBundleDisplayName") as! String
  31. let app_Version = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
  32. /// 输入框 placeholder text color
  33. var Color_placeholder_text = UIColorFromHex(rgbValue: 0x515151)
  34. /// 导航栏背景色
  35. var Color_Navigation_BarTint = UIColorFromHex(rgbValue: 0x1882D2)
  36. /// Tabbar Item normal
  37. var Color_BarItem_Normal = UIColorFromHex(rgbValue: 0x797296)
  38. /// Tabbar Item Selected
  39. var Color_BarItem_Selected = UIColorFromHex(rgbValue: 0x797296)
  40. /// Color_CollectionCell_Border
  41. var Color_CollectionCell_Border = UIColorFromHex(rgbValue: 0xF0F0F0)
  42. /// 空值转"--"、0、0.0
  43. func formatStr(str:Any) ->String{
  44. if let str = str as? Double {
  45. return str == 0.0 ? "--" : String(str)
  46. } else if let str = str as? String {
  47. return str == "null" ? "--" : str
  48. } else if let str = str as? Int {
  49. return str == 0 ? "--" : String(str)
  50. } else{
  51. return "--"
  52. }
  53. }
  54. /// 横竖屏
  55. func setNewOrientation(fullScreen: Bool) {
  56. if fullScreen { /// 横屏
  57. let resetOrientationTargert = NSNumber(integerLiteral: UIInterfaceOrientation.unknown.rawValue)
  58. UIDevice.current.setValue(resetOrientationTargert, forKey: "orientation")
  59. let orientationTarget = NSNumber(integerLiteral: UIInterfaceOrientation.landscapeLeft.rawValue)
  60. UIDevice.current.setValue(orientationTarget, forKey: "orientation")
  61. } else { /// 竖屏
  62. let resetOrientationTargert = NSNumber(integerLiteral: UIInterfaceOrientation.unknown.rawValue)
  63. UIDevice.current.setValue(resetOrientationTargert, forKey: "orientation")
  64. let orientationTarget = NSNumber(integerLiteral: UIInterfaceOrientation.portrait.rawValue)
  65. UIDevice.current.setValue(orientationTarget, forKey: "orientation")
  66. }
  67. }
  68. /// ColorFromHex
  69. /// - Parameters:
  70. /// - rgbValue: rgbValue
  71. /// - alpha: alpha
  72. /// - Returns: UIColor
  73. func UIColorFromHex(rgbValue:Int, alpha:CGFloat = 1.0) -> UIColor {
  74. return UIColor.init(red: (CGFloat)((Float)((rgbValue & 0xFF0000) >> 16))/255.0,
  75. green: ((CGFloat)((rgbValue & 0xFF00) >> 8))/255.0,
  76. blue: ((CGFloat)(rgbValue & 0xFF))/255.0 , alpha: alpha)
  77. }
  78. // MARK: - 数组去重方法
  79. extension Array {
  80. /// 该函数的参数filterCall是一个带返回值的闭包,传入模型T,返回一个E类型
  81. func handleFilter<E: Equatable>(_ filterCall: (Array.Element) -> E) -> [Array.Element] {
  82. var temp = [Array.Element]()
  83. for model in self {
  84. /// 调用filterCall,获得需要用来判断的属性E
  85. let identifer = filterCall(model)
  86. /// 此处利用map函数 来将model类型数组转换成E类型的数组,以此来判断identifer 是否已经存在,如不存在则将model添加进temp
  87. if !temp.compactMap( { filterCall($0) } ).contains(identifer) {
  88. temp.append(model)
  89. }
  90. }
  91. return temp
  92. }
  93. }
  94. /// swift字典转模型
  95. ///
  96. /// - Parameters:
  97. /// - type: 模型类
  98. /// - KeyAndValues: 字典
  99. /// - Returns: 模型对象
  100. /// - Throws: 转换失败 && 解码失败
  101. func jsonModel<T>(_ type: T.Type, with KeyAndValues: [String: Any]) -> T? where T: Decodable {
  102. let jsonObject = try? JSONSerialization.data(withJSONObject: KeyAndValues, options: [])
  103. return try? JSONDecoder().decode(type, from: jsonObject!)
  104. }