evgenyneu / swift-badge

A badge view for iOS/tvOS written in Swift
MIT License
394 stars 107 forks source link

BadgeView does not seem to resize depending on content #17

Open ghost opened 6 years ago

ghost commented 6 years ago

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

evgenyneu commented 6 years ago

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?

ghost commented 6 years ago

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 notifications@github.com:

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.

evgenyneu commented 6 years ago

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?
ghost commented 6 years ago

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 notifications@github.com:

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.

evgenyneu commented 6 years ago

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

vin047 commented 3 years ago

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