SectionHeader.swift 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //
  2. // SectionHeader.swift
  3. // DropdownMenu
  4. //
  5. // Created by WangWei on 2016/10/9.
  6. // Copyright © 2016年 teambition. All rights reserved.
  7. //
  8. open class SectionHeader: UIView {
  9. var titleLabel: UILabel!
  10. var style: SectionHeaderStyle = SectionHeaderStyle()
  11. convenience init(style: SectionHeaderStyle) {
  12. self.init(frame: CGRect.zero)
  13. self.style = style
  14. commonInit()
  15. }
  16. override init(frame: CGRect) {
  17. super.init(frame: frame)
  18. }
  19. required public init?(coder aDecoder: NSCoder) {
  20. super.init(coder: aDecoder)
  21. }
  22. func commonInit() {
  23. titleLabel = UILabel()
  24. titleLabel.translatesAutoresizingMaskIntoConstraints = false
  25. titleLabel.font = style.font
  26. titleLabel.textColor = style.textColor
  27. backgroundColor = style.backgroundColor
  28. addSubview(titleLabel)
  29. updateTitleLabelConstraint()
  30. }
  31. func updateTitleLabelConstraint() {
  32. if #available(iOS 11.0, *) {
  33. let leftConstraint = NSLayoutConstraint(item: titleLabel as Any, attribute: .left, relatedBy: .equal, toItem: safeAreaLayoutGuide, attribute: .left, multiplier: 1.0, constant: style.bottomPadding)
  34. NSLayoutConstraint.activate([leftConstraint])
  35. } else {
  36. // Fallback on earlier versions
  37. let constraints = NSLayoutConstraint.constraints(withVisualFormat: "H:|-leftPadding-[titleLabel]->=20-|", options: [], metrics: ["leftPadding": style.leftPadding], views: ["titleLabel": titleLabel as Any])
  38. addConstraints(constraints)
  39. }
  40. if style.shouldTitleCenterVertically {
  41. let centerY = NSLayoutConstraint(item: titleLabel as Any, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.0, constant: 0)
  42. addConstraint(centerY)
  43. } else {
  44. let vConstraints = NSLayoutConstraint(item: titleLabel as Any, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: style.bottomPadding)
  45. addConstraint(vConstraints)
  46. }
  47. }
  48. }
  49. public struct SectionHeaderStyle {
  50. /// leftPadding for title label, default is `20`
  51. public var leftPadding: CGFloat = 20
  52. /// bottom padding for title label, default is `10`,
  53. /// will be ignored when `shouldTitleCenterVertically` is `true`
  54. public var bottomPadding: CGFloat = 10
  55. /// should title label center in axis Y, default is `true`
  56. public var shouldTitleCenterVertically: Bool = true
  57. /// title label font, default is `UIFont.systemFont(ofSize: 14)`
  58. public var font: UIFont = UIFont.systemFont(ofSize: 14)
  59. /// title label textColor, default is A6A6A6
  60. public var textColor: UIColor = UIColor(red: 166.0/255.0, green: 166.0/255.0, blue: 166.0/255.0, alpha: 1.0)
  61. /// backgroundColor for header, default is F2F2F2
  62. public var backgroundColor: UIColor = UIColor(red: 242.0/255.0, green: 242.0/255.0, blue: 242.0/255.0, alpha: 1.0)
  63. public init() {
  64. }
  65. }