IconSegment.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // IconSegment.swift
  3. // BetterSegmentedControl
  4. //
  5. // Created by George Marmaridis on 10/02/2018.
  6. //
  7. import Foundation
  8. open class IconSegment: BetterSegmentedControlSegment {
  9. // MARK: Constants
  10. private struct DefaultValues {
  11. static let normalBackgroundColor: UIColor = .clear
  12. static let selectedBackgroundColor: UIColor = .clear
  13. }
  14. // MARK: Properties
  15. public var icon: UIImage
  16. public var iconSize: CGSize
  17. public var normalIconTintColor: UIColor
  18. public var normalBackgroundColor: UIColor
  19. public var selectedIconTintColor: UIColor
  20. public var selectedBackgroundColor: UIColor
  21. // MARK: Lifecycle
  22. public init(icon: UIImage,
  23. iconSize: CGSize,
  24. normalBackgroundColor: UIColor? = nil,
  25. normalIconTintColor: UIColor,
  26. selectedBackgroundColor: UIColor? = nil,
  27. selectedIconTintColor: UIColor) {
  28. self.icon = icon.withRenderingMode(.alwaysTemplate)
  29. self.iconSize = iconSize
  30. self.normalBackgroundColor = normalBackgroundColor ?? DefaultValues.normalBackgroundColor
  31. self.normalIconTintColor = normalIconTintColor
  32. self.selectedBackgroundColor = selectedBackgroundColor ?? DefaultValues.selectedBackgroundColor
  33. self.selectedIconTintColor = selectedIconTintColor
  34. }
  35. // MARK: BetterSegmentedControlSegment
  36. public lazy var normalView: UIView = {
  37. return view(withIcon: icon,
  38. iconSize: iconSize,
  39. backgroundColor: normalBackgroundColor,
  40. iconTintColor: normalIconTintColor)
  41. }()
  42. public lazy var selectedView: UIView = {
  43. return view(withIcon: icon,
  44. iconSize: iconSize,
  45. backgroundColor: selectedBackgroundColor,
  46. iconTintColor: selectedIconTintColor)
  47. }()
  48. private func view(withIcon icon: UIImage,
  49. iconSize: CGSize,
  50. backgroundColor: UIColor,
  51. iconTintColor: UIColor) -> UIView {
  52. let view = UIView()
  53. view.backgroundColor = backgroundColor
  54. let imageView = UIImageView(image: icon)
  55. imageView.frame = CGRect(x: 0,
  56. y: 0,
  57. width: iconSize.width,
  58. height: iconSize.height)
  59. imageView.contentMode = .scaleAspectFit
  60. imageView.tintColor = iconTintColor
  61. imageView.autoresizingMask = [.flexibleTopMargin,
  62. .flexibleLeftMargin,
  63. .flexibleRightMargin,
  64. .flexibleBottomMargin]
  65. view.addSubview(imageView)
  66. return view
  67. }
  68. }
  69. public extension IconSegment {
  70. class func segments(withIcons icons: [UIImage],
  71. iconSize: CGSize,
  72. normalBackgroundColor: UIColor? = nil,
  73. normalIconTintColor: UIColor,
  74. selectedBackgroundColor: UIColor? = nil,
  75. selectedIconTintColor: UIColor) -> [BetterSegmentedControlSegment] {
  76. return icons.map {
  77. IconSegment(icon: $0,
  78. iconSize: iconSize,
  79. normalBackgroundColor: normalBackgroundColor,
  80. normalIconTintColor: normalIconTintColor,
  81. selectedBackgroundColor: selectedBackgroundColor,
  82. selectedIconTintColor: selectedIconTintColor)
  83. }
  84. }
  85. }