Icon.swift 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //
  2. // Icon.swift
  3. // QuickTableViewController
  4. //
  5. // Created by Ben on 01/09/2015.
  6. // Copyright (c) 2015 bcylin.
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in all
  16. // copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. // SOFTWARE.
  25. //
  26. import UIKit
  27. /// A struct that represents the image used in a row.
  28. public enum Icon: Equatable {
  29. /// Icon with an image of the given name for the normal state.
  30. /// The "-highlighted" suffix is appended to the name for the highlighted image.
  31. case named(String)
  32. /// Icon with an image for the normal state.
  33. case image(UIImage)
  34. /// Icon with images for the normal and highlighted states.
  35. case images(normal: UIImage, highlighted: UIImage)
  36. /// The image for the normal state.
  37. public var image: UIImage? {
  38. switch self {
  39. case let .named(name):
  40. return UIImage(named: name)
  41. case let .image(image):
  42. return image
  43. case let .images(normal: image, highlighted: _):
  44. return image
  45. }
  46. }
  47. /// The image for the highlighted state.
  48. public var highlightedImage: UIImage? {
  49. switch self {
  50. case let .named(name):
  51. return UIImage(named: name + "-highlighted")
  52. case .image:
  53. return nil
  54. case let .images(normal: _, highlighted: image):
  55. return image
  56. }
  57. }
  58. }