@@ -30,19 +30,20 @@ final class BadgeButton: UIButton {
30
30
badgeLabel? . isHidden = false
31
31
badgeLabel? . text = text
32
32
badgeLabel? . font = UIFont . boldSystemFont ( ofSize: 12.0 )
33
- badgeLabel? . edgeInsets = UIEdgeInsets ( top: 4 , left: 4 , bottom: 4 , right: 4 )
33
+ badgeLabel? . edgeInsets = UIEdgeInsets ( top: 4 , left: 2 , bottom: 4 , right: 2 )
34
34
badgeLabel? . layer. cornerRadius = 10
35
35
accessibilityValue = Strings . Localizable. notifications
36
+
37
+ if let badgeLabel {
38
+ badgeLabel. widthAnchor. constraint ( greaterThanOrEqualToConstant: calculateWidthThatFits ( label: badgeLabel) ) . isActive = true
39
+ }
36
40
}
37
41
38
42
// MARK: - Privates
39
43
40
44
private func layoutBadgeLabel( _ badgeLabel: UILabel ) {
41
45
badgeLabel. translatesAutoresizingMaskIntoConstraints = false
42
- NSLayoutConstraint . activate ( [
43
- badgeLabel. widthAnchor. constraint ( greaterThanOrEqualToConstant: 20 ) ,
44
- badgeLabel. heightAnchor. constraint ( equalToConstant: 20 )
45
- ] )
46
+ badgeLabel. heightAnchor. constraint ( equalToConstant: 20 ) . isActive = true
46
47
}
47
48
48
49
private func setupBadgeLabel( with trait: UITraitCollection ) -> UILabel {
@@ -72,4 +73,11 @@ final class BadgeButton: UIButton {
72
73
badgeLabel. centerXAnchor. constraint ( equalTo: trailingAnchor)
73
74
] )
74
75
}
76
+
77
+ private func calculateWidthThatFits( label: UILabel ) -> CGFloat {
78
+ let minimumWidth : CGFloat = 20
79
+ let fitSize = CGSize ( width: UIScreen . main. bounds. width, height: . greatestFiniteMagnitude)
80
+ let fitWidth = label. sizeThatFits ( fitSize) . width
81
+ return fitWidth > minimumWidth ? fitWidth : minimumWidth
82
+ }
75
83
}
0 commit comments