Skip to content

Commit 4df1a5c

Browse files
committed
iOS-6850: Calculate the right size to fit the badge label depending on its text
1 parent 6e2551d commit 4df1a5c

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

iMEGA/Utils/Shared Views/Views/BadgeButton.swift

+13-5
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,20 @@ final class BadgeButton: UIButton {
3030
badgeLabel?.isHidden = false
3131
badgeLabel?.text = text
3232
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)
3434
badgeLabel?.layer.cornerRadius = 10
3535
accessibilityValue = Strings.Localizable.notifications
36+
37+
if let badgeLabel {
38+
badgeLabel.widthAnchor.constraint(greaterThanOrEqualToConstant: calculateWidthThatFits(label: badgeLabel)).isActive = true
39+
}
3640
}
3741

3842
// MARK: - Privates
3943

4044
private func layoutBadgeLabel(_ badgeLabel: UILabel) {
4145
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
4647
}
4748

4849
private func setupBadgeLabel(with trait: UITraitCollection) -> UILabel {
@@ -72,4 +73,11 @@ final class BadgeButton: UIButton {
7273
badgeLabel.centerXAnchor.constraint(equalTo: trailingAnchor)
7374
])
7475
}
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+
}
7583
}

0 commit comments

Comments
 (0)