KMAdvertisementModelTransition.swift 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // KMAdvertisementModelTansition.swift
  3. // KMAdvertisement
  4. //
  5. // Created by lizhe on 2022/12/2.
  6. //
  7. #if os(OSX)
  8. import AppKit
  9. #elseif os(iOS)
  10. import UIKit
  11. #endif
  12. open class KMAdvertisementModelTransition: NSObject {
  13. //MARK: 模型转换
  14. //颜色模型转换
  15. static func transitionColor(color: KMAdvertisementModel.Color, highlight:Bool) -> UIColor {
  16. var result = UIColor.clear
  17. if highlight {
  18. let darkHex = color.height?.dark?.hex
  19. let darkAlpha = color.height?.dark?.alpha
  20. let universalHex = color.height?.universal?.hex
  21. let universalAlpha = color.height?.universal?.alpha
  22. let light = NSColor.init(hex: universalHex ?? "").withAlphaComponent(CGFloat(universalAlpha ?? 1))
  23. let dark = NSColor.init(hex: darkHex ?? universalHex ?? "").withAlphaComponent(CGFloat(darkAlpha ?? universalAlpha ?? 1))
  24. result = KMAdvertisementConfig.adapterDark(light: light , dark: dark)
  25. } else {
  26. let darkHex = color.normal?.dark?.hex
  27. let darkAlpha = color.normal?.dark?.alpha
  28. let universalHex = color.normal?.universal?.hex
  29. let universalAlpha = color.normal?.universal?.alpha
  30. let light = NSColor.init(hex: universalHex ?? "").withAlphaComponent(CGFloat(universalAlpha ?? 1))
  31. let dark = NSColor.init(hex: darkHex ?? universalHex ?? "").withAlphaComponent(CGFloat(darkAlpha ?? universalAlpha ?? 1))
  32. result = KMAdvertisementConfig.adapterDark(light: light, dark: dark)
  33. }
  34. return result
  35. }
  36. //图片转换
  37. static func transitionImagePath(image: KMAdvertisementModel.Image, highlight:Bool) -> String {
  38. var result = ""
  39. if highlight {
  40. if image.height != nil {
  41. result = transitionLanguage(langeuage: image.height!)
  42. }
  43. } else {
  44. if image.normal != nil {
  45. result = transitionLanguage(langeuage: image.normal!)
  46. }
  47. }
  48. return result
  49. }
  50. //多语转换
  51. static func transitionLanguage(langeuage: KMAdvertisementModel.Language?) -> String {
  52. var result = ""
  53. let languageConfig = KMAdvertisementManager.manager.configuration.language
  54. if languageConfig != .unknow {
  55. if languageConfig == .en {
  56. result = langeuage?.en ?? ""
  57. } else if languageConfig == .zhCN {
  58. result = langeuage?.zh_CN ?? ""
  59. } else if languageConfig == .zhTW {
  60. result = langeuage?.zh_TW ?? ""
  61. }
  62. } else {
  63. let string = KMAdvertisementModelTransition.getLanguageType()
  64. if string == "zh-Hans-CN" {
  65. result = langeuage?.zh_CN ?? ""
  66. } else if string == "zh-Hans-TW" {
  67. result = langeuage?.zh_TW ?? ""
  68. } else {
  69. result = langeuage?.en ?? ""
  70. }
  71. }
  72. return result
  73. }
  74. static func getLanguageType() -> String {
  75. #if os(iOS)
  76. let def = UserDefaults.standard
  77. let allLanguages: [String] = def.object(forKey: "AppleLanguages") as! [String]
  78. let chooseLanguage = allLanguages.first
  79. return chooseLanguage ?? "en"
  80. #elseif os(OSX)
  81. let locale = NSLocale.current.languageCode
  82. let chooseLanguage = Locale.preferredLanguages[0]
  83. return chooseLanguage
  84. #endif
  85. }
  86. }