O2ThemeManager+Plist.swift 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //
  2. // ThemeManager+Plist.swift
  3. // SwiftTheme
  4. //
  5. // Created by Gesen on 16/9/18.
  6. // Copyright © 2016年 Gesen. All rights reserved.
  7. //
  8. import UIKit
  9. @objc extension O2ThemeManager {
  10. public class func value(for keyPath: String) -> Any? {
  11. return currentTheme?.value(forKeyPath: keyPath)
  12. }
  13. public class func string(for keyPath: String) -> String? {
  14. guard let string = currentTheme?.value(forKeyPath: keyPath) as? String else {
  15. print("SwiftTheme WARNING: Not found string key path: \(keyPath)")
  16. return nil
  17. }
  18. return string
  19. }
  20. public class func number(for keyPath: String) -> NSNumber? {
  21. guard let number = currentTheme?.value(forKeyPath: keyPath) as? NSNumber else {
  22. print("SwiftTheme WARNING: Not found number key path: \(keyPath)")
  23. return nil
  24. }
  25. return number
  26. }
  27. public class func dictionary(for keyPath: String) -> NSDictionary? {
  28. guard let dict = currentTheme?.value(forKeyPath: keyPath) as? NSDictionary else {
  29. print("SwiftTheme WARNING: Not found dictionary key path: \(keyPath)")
  30. return nil
  31. }
  32. return dict
  33. }
  34. public class func color(for keyPath: String) -> UIColor? {
  35. guard let rgba = string(for: keyPath) else { return nil }
  36. guard let color = try? UIColor(rgba_throws: rgba) else {
  37. print("SwiftTheme WARNING: Not convert rgba \(rgba) at key path: \(keyPath)")
  38. return nil
  39. }
  40. return color
  41. }
  42. public class func image(for keyPath: String) -> UIImage? {
  43. guard let imageName = string(for: keyPath) else { return nil }
  44. if let filePath = currentThemePath?.URL?.appendingPathComponent(imageName).path {
  45. guard let image = UIImage(contentsOfFile: filePath) else {
  46. print("SwiftTheme WARNING: Not found image at file path: \(filePath)")
  47. return nil
  48. }
  49. return image
  50. } else {
  51. guard let image = UIImage(named: imageName) else {
  52. print("SwiftTheme WARNING: Not found image name at main bundle: \(imageName)")
  53. return nil
  54. }
  55. return image
  56. }
  57. }
  58. }