swift-badge icon indicating copy to clipboard operation
swift-badge copied to clipboard

BadgeView does not seem to resize depending on content

Open ghost opened this issue 7 years ago • 6 comments

Xcode 9.1 /Swift 4

Is it intendet that the badgeView does not resize dynamically regarding its content?

I would expect a widget to do that

ghost avatar Nov 20 '17 17:11 ghost

Hello @camdata, the badge should resize to according the size of the text. If it doesn't then there must be a bug. Can you tell me about your issue?

evgenyneu avatar Nov 20 '17 21:11 evgenyneu

Hi Evgenii,

thanks for your reply. First i do like your framework and i implemented the following work around:

func setBadge(count: Int) { badgeView.text = String(count) badgeView.alpha = count == 0 ? 0 : 1 badgeView.bounds.size.width = 10 + badgeView.text!.widthOfString(usingFont: badgeView.font) }

using:

  func widthOfString(usingFont font: UIFont) -> CGFloat {
        let fontAttributes = [NSAttributedStringKey.font: font]
        let size = self.size(withAttributes: fontAttributes)
        return size.width
    }

Hope that helps,

Mike

Am 20.11.2017 um 22:31 schrieb Evgenii Neumerzhitckii [email protected]:

Hello @camdata https://github.com/camdata, the badge should resize to according the size of the text. If it doesn't then there must be a bug. Can you tell me about your issue?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/evgenyneu/swift-badge/issues/17#issuecomment-345838380, or mute the thread https://github.com/notifications/unsubscribe-auth/AfUtkHQn2t2TnVYkwY-sugTuJ91AnD9gks5s4e-mgaJpZM4QkpBB.

ghost avatar Nov 21 '17 10:11 ghost

Thanks for the workaround. I would like to know why the badge does not resize in the first place. As you probably noticed, the badge is just a UILabel. Therefore, it should resize itself automatically. It should not require setting its size explicitly.

Can you describe your use case, so we can fix the problem?

  1. How do you use and position the badge?
  2. When do you update it?
  3. Do update the badge from the main queue?

evgenyneu avatar Nov 21 '17 11:11 evgenyneu

UILables do only autoresize if line number are set to zero (from iOS 6+ on) - maybe thats the point? You could also use the sizeToFit method internally inside badgeView. The is no UIinspactable i could find regarding the line number. Mayber i am doing something else wrong.

I used Storyboard to place a badgeView inside of a VC. The method i sent you is the only place where i update the badgeView. I do not redraw the badgeView manually somewhere.

Am 21.11.2017 um 12:13 schrieb Evgenii Neumerzhitckii [email protected]:

Thanks for the workaround. I would like to know why the badge does not resize in the first place. As you probably noticed, the badge is just a UILabel. Therefore, it should resize itself automatically. It should not require setting its size explicitly.

Can you describe your use case, so we can fix the problem?

How do you use and position the badge? When do you update it? Do update the badge from the main queue? — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/evgenyneu/swift-badge/issues/17#issuecomment-345995253, or mute the thread https://github.com/notifications/unsubscribe-auth/AfUtkGeMyT9E7xVCmQws642X_Nb5HTcFks5s4rBigaJpZM4QkpBB.

ghost avatar Nov 21 '17 11:11 ghost

I could not reproduce your problem. I've made a demo app with a badge and a stepper that changes the number. The badge autoresize automatically, when "Lines" property is 1 (which is default) and 0.

Do you update the badge number in the main queue?

Please find the demo attached.

DemoBadge.zip

evgenyneu avatar Nov 21 '17 11:11 evgenyneu

You can also use sizeToFit() after setting the title.

vin047 avatar Sep 28 '21 00:09 vin047