LabelSegment.swift 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // LabelSegment.swift
  3. // BetterSegmentedControl
  4. //
  5. // Created by George Marmaridis on 08/10/2017.
  6. //
  7. import Foundation
  8. open class LabelSegment: BetterSegmentedControlSegment {
  9. // MARK: Constants
  10. private struct DefaultValues {
  11. static let normalBackgroundColor: UIColor = .clear
  12. static let normalTextColor: UIColor = .white
  13. static let selectedBackgroundColor: UIColor = .clear
  14. static let selectedTextColor: UIColor = .black
  15. static let font: UIFont = UILabel().font
  16. }
  17. // MARK: Properties
  18. public var text: String?
  19. public var normalFont: UIFont
  20. public var normalTextColor: UIColor
  21. public var normalBackgroundColor: UIColor
  22. public var selectedFont: UIFont
  23. public var selectedTextColor: UIColor
  24. public var selectedBackgroundColor: UIColor
  25. // MARK: Lifecycle
  26. public init(text: String? = nil,
  27. normalBackgroundColor: UIColor? = nil,
  28. normalFont: UIFont? = nil,
  29. normalTextColor: UIColor? = nil,
  30. selectedBackgroundColor: UIColor? = nil,
  31. selectedFont: UIFont? = nil,
  32. selectedTextColor: UIColor? = nil) {
  33. self.text = text
  34. self.normalBackgroundColor = normalBackgroundColor ?? DefaultValues.normalBackgroundColor
  35. self.normalFont = normalFont ?? DefaultValues.font
  36. self.normalTextColor = normalTextColor ?? DefaultValues.normalTextColor
  37. self.selectedBackgroundColor = selectedBackgroundColor ?? DefaultValues.selectedBackgroundColor
  38. self.selectedFont = selectedFont ?? DefaultValues.font
  39. self.selectedTextColor = selectedTextColor ?? DefaultValues.selectedTextColor
  40. }
  41. // MARK: BetterSegmentedControlSegment
  42. public lazy var normalView: UIView = {
  43. return label(withText: text,
  44. backgroundColor: normalBackgroundColor,
  45. font: normalFont,
  46. textColor: normalTextColor)
  47. }()
  48. public lazy var selectedView: UIView = {
  49. return label(withText: text,
  50. backgroundColor: selectedBackgroundColor,
  51. font: selectedFont,
  52. textColor: selectedTextColor)
  53. }()
  54. private func label(withText text: String?,
  55. backgroundColor: UIColor,
  56. font: UIFont,
  57. textColor: UIColor) -> UILabel {
  58. let label = UILabel()
  59. label.text = text
  60. label.backgroundColor = backgroundColor
  61. label.font = font
  62. label.textColor = textColor
  63. label.lineBreakMode = .byTruncatingTail
  64. label.textAlignment = .center
  65. return label
  66. }
  67. }
  68. public extension LabelSegment {
  69. class func segments(withTitles titles: [String],
  70. normalBackgroundColor: UIColor? = nil,
  71. normalFont: UIFont? = nil,
  72. normalTextColor: UIColor? = nil,
  73. selectedBackgroundColor: UIColor? = nil,
  74. selectedFont: UIFont? = nil,
  75. selectedTextColor: UIColor? = nil) -> [BetterSegmentedControlSegment] {
  76. return titles.map {
  77. LabelSegment(text: $0,
  78. normalBackgroundColor: normalBackgroundColor,
  79. normalFont: normalFont,
  80. normalTextColor: normalTextColor,
  81. selectedBackgroundColor: selectedBackgroundColor,
  82. selectedFont: selectedFont,
  83. selectedTextColor: selectedTextColor)
  84. }
  85. }
  86. }