O2ThemeManager+Index.swift 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. //
  2. // ThemeManager+Index.swift
  3. // SwiftTheme
  4. //
  5. // Created by Gesen on 16/9/18.
  6. // Copyright © 2016年 Gesen. All rights reserved.
  7. //
  8. import UIKit
  9. extension O2ThemeManager {
  10. public class func colorElement(for array: [String]) -> UIColor? {
  11. guard let rgba = element(for: array) else { return nil }
  12. guard let color = try? UIColor(rgba_throws: rgba as String) else {
  13. print("SwiftTheme WARNING: Not convert rgba \(rgba) in array: \(array)[\(currentThemeIndex)]")
  14. return nil
  15. }
  16. return color
  17. }
  18. public class func imageElement(for array: [String]) -> UIImage? {
  19. guard let imageName = element(for: array) else { return nil }
  20. guard let image = UIImage(named: imageName as String) else {
  21. print("SwiftTheme WARNING: Not found image name '\(imageName)' in array: \(array)[\(currentThemeIndex)]")
  22. return nil
  23. }
  24. return image
  25. }
  26. public class func element<T>(for array: [T]) -> T? {
  27. let index = O2ThemeManager.currentThemeIndex
  28. guard array.indices ~= index else {
  29. print("SwiftTheme WARNING: Not found element in array: \(array)[\(currentThemeIndex)]")
  30. return nil
  31. }
  32. return array[index]
  33. }
  34. }