Skip to content

Commit

Permalink
iOS-6850: Calculate the right size to fit the badge label depending o…
Browse files Browse the repository at this point in the history
…n its text
  • Loading branch information
rgmez committed Jan 25, 2023
1 parent 6e2551d commit 4df1a5c
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions iMEGA/Utils/Shared Views/Views/BadgeButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,20 @@ final class BadgeButton: UIButton {
badgeLabel?.isHidden = false
badgeLabel?.text = text
badgeLabel?.font = UIFont.boldSystemFont(ofSize: 12.0)
badgeLabel?.edgeInsets = UIEdgeInsets(top: 4, left: 4, bottom: 4, right: 4)
badgeLabel?.edgeInsets = UIEdgeInsets(top: 4, left: 2, bottom: 4, right: 2)
badgeLabel?.layer.cornerRadius = 10
accessibilityValue = Strings.Localizable.notifications

if let badgeLabel {
badgeLabel.widthAnchor.constraint(greaterThanOrEqualToConstant: calculateWidthThatFits(label: badgeLabel)).isActive = true
}
}

// MARK: - Privates

private func layoutBadgeLabel(_ badgeLabel: UILabel) {
badgeLabel.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
badgeLabel.widthAnchor.constraint(greaterThanOrEqualToConstant: 20),
badgeLabel.heightAnchor.constraint(equalToConstant: 20)
])
badgeLabel.heightAnchor.constraint(equalToConstant: 20).isActive = true
}

private func setupBadgeLabel(with trait: UITraitCollection) -> UILabel {
Expand Down Expand Up @@ -72,4 +73,11 @@ final class BadgeButton: UIButton {
badgeLabel.centerXAnchor.constraint(equalTo: trailingAnchor)
])
}

private func calculateWidthThatFits(label: UILabel) -> CGFloat {
let minimumWidth: CGFloat = 20
let fitSize = CGSize(width: UIScreen.main.bounds.width, height: .greatestFiniteMagnitude)
let fitWidth = label.sizeThatFits(fitSize).width
return fitWidth > minimumWidth ? fitWidth : minimumWidth
}
}

0 comments on commit 4df1a5c

Please sign in to comment.