@@ -13,6 +13,7 @@ class LGSegment {
1313 var contentView : UIView = {
1414 let view = UIView ( frame: CGRect ( x: 0 , y: 0 , width: 100 , height: 50 ) )
1515 view. backgroundColor = . clear
16+ view. layer. zPosition = 0
1617 return view
1718 } ( )
1819 var titleLabel : UILabel = {
@@ -26,15 +27,32 @@ class LGSegment {
2627 let view = UIView ( frame: CGRect ( x: 0 , y: 0 , width: 100 , height: 50 ) )
2728 view. clipsToBounds = true
2829 view. translatesAutoresizingMaskIntoConstraints = false
30+ view. layer. zPosition = 1
2931 return view
3032 } ( )
31-
33+ lazy var badgeView : UIView = {
34+ let view = UIView ( frame: CGRect ( x: 0 , y: 0 , width: 16 , height: 16 ) )
35+ view. backgroundColor = options. badgeColor. background
36+ view. translatesAutoresizingMaskIntoConstraints = false
37+ view. layer. zPosition = 2
38+ return view
39+ } ( )
40+ lazy var badgeLabel : UILabel = {
41+ let label = UILabel ( frame: CGRect ( x: 0 , y: 0 , width: 16 , height: 16 ) )
42+ label. textColor = options. badgeColor. text
43+ label. font = UIFont . systemFont ( ofSize: 13 , weight: . medium)
44+ label. textAlignment = . center
45+ label. sizeToFit ( )
46+ label. translatesAutoresizingMaskIntoConstraints = false
47+ return label
48+ } ( )
3249 lazy var tapView : UIView = {
3350 let view = UIView ( frame: CGRect ( x: 0 , y: 0 , width: 100 , height: 50 ) )
3451 view. backgroundColor = . clear
3552 view. translatesAutoresizingMaskIntoConstraints = false
3653 let tap = UITapGestureRecognizer ( target: self , action: #selector( handleTap) )
3754 view. addGestureRecognizer ( tap)
55+ view. layer. zPosition = 3
3856 return view
3957 } ( )
4058
@@ -51,10 +69,17 @@ class LGSegment {
5169
5270 var options = LGSegmentOptions ( )
5371
72+ public var badgeCount : Int ? {
73+ didSet {
74+ updateAppearance ( with: options)
75+ }
76+ }
77+
5478 var delegate : LGSegmentDelegate ?
5579
56- public init ( title: String ) {
80+ public init ( title: String , badgeCount : Int ? = nil ) {
5781 self . title = title
82+ self . badgeCount = badgeCount
5883
5984 updateAppearance ( with: options, animated: false )
6085
@@ -63,6 +88,8 @@ class LGSegment {
6388
6489 private func setupConstraints( ) {
6590 backgroundView. addSubview ( titleLabel)
91+ badgeView. addSubview ( badgeLabel)
92+ contentView. addSubview ( badgeView)
6693 contentView. addSubview ( backgroundView)
6794 contentView. addSubview ( tapView)
6895
@@ -78,6 +105,19 @@ class LGSegment {
78105 titleLabel. topAnchor. constraint ( greaterThanOrEqualTo: backgroundView. topAnchor, constant: 6 ) . isActive = true
79106 titleLabel. bottomAnchor. constraint ( greaterThanOrEqualTo: backgroundView. bottomAnchor, constant: 6 ) . isActive = true
80107
108+ badgeLabel. centerYAnchor . constraint ( equalTo: badgeView. centerYAnchor) . isActive = true
109+ badgeLabel. leadingAnchor . constraint ( equalTo: badgeView. leadingAnchor , constant: 4 ) . isActive = true
110+ badgeLabel. trailingAnchor. constraint ( equalTo: badgeView. trailingAnchor, constant: - 4 ) . isActive = true
111+ badgeLabel. topAnchor . constraint ( equalTo: badgeView. topAnchor, constant: 0.5 ) . isActive = true
112+ badgeLabel. bottomAnchor . constraint ( equalTo: badgeView. bottomAnchor, constant: - 0.5 ) . isActive = true
113+
114+ badgeView. leadingAnchor . constraint ( equalTo: titleLabel. trailingAnchor, constant: 0 ) . isActive = true
115+ badgeView. bottomAnchor . constraint ( equalTo: titleLabel. topAnchor , constant: 6 ) . isActive = true
116+ let badgeViewSize : CGFloat = 16
117+ badgeView. heightAnchor . constraint ( greaterThanOrEqualToConstant: badgeViewSize) . isActive = true
118+ badgeView. widthAnchor . constraint ( greaterThanOrEqualToConstant: badgeViewSize) . isActive = true
119+ badgeView. layer. cornerRadius = badgeViewSize/ 2
120+
81121 tapView. leadingAnchor. constraint ( equalTo: contentView. leadingAnchor) . isActive = true
82122 tapView. trailingAnchor. constraint ( equalTo: contentView. trailingAnchor) . isActive = true
83123 tapView. topAnchor. constraint ( equalTo: contentView. topAnchor) . isActive = true
@@ -95,7 +135,9 @@ class LGSegment {
95135 // others
96136 self . backgroundView. layer. cornerRadius = options. cornerRadius
97137 self . titleLabel. font = options. font
138+ self . badgeView. isHidden = self . badgeCount == nil
98139 }
140+ badgeLabel. text = badgeCount? . string ?? " "
99141 }
100142
101143 @objc private func handleTap( ) {
0 commit comments