Dev:SBIconBadgeView

From The Apple Wiki

SBIconBadgeView is a class representing the notification badge of an app. It has a subview, SBDarkeningImageView, that is the badge background image, which has a UIImageView subview that as an image of the badge count.

(Pre-iOS 16) Background Image Caching

Pre-iOS 16, SBIconBadgeView will cache the background image to `/var/mobile/Library/Caches/MappedImageCache/Persistent/SBIconBadgeView.BadgeBackground:26:26.cpbitmap`. Be aware that `26` is the default size of the badge view, and therefore if badge size is changed, say through a tweak, then it will cache the different badge size, although normal behavior will never have the default badge size change from 26.

iOS 16 Changes

In iOS 15, SBIconBadgeView's layoutSubviews method checks if the _backgroundImageTuple ivar is set (if not, it generates the image via [self _checkoutBackgroundImageTuple] and saves _backgroundImageTuple, and sets the image of _backgroundView to the image generated). Then it sets the frame, and if the badge has a _textView or _incomingTextView, will lay them out via [self _layOutTextImageView]. Basically, iOS 15- uses an image to represent the badge view. However, iOS 16 changes this behavior.


While iOS 16 does still use a SBDarkeningImageView to represent the background, it no longer actually uses an image. Instead, it sets a backgroundColor to `[SBIconBadgeView badgeBackgroundColor]`, which is `[UIColor systemRedColor]`. This would result in a square - but now in the SBIconBadgeView layoutSubviews method, it will constantly set the corner radius of the background view to be a circle.

  1. Label Image Caching

While iOS 16 has gone with background image caching, (currently, at the time of writing) images are still used to display the label on the badge. Here's an example of how the `_checkoutImageForText` method generates its names (text arg is the badge count):

(the first arg (text:), arg0, is NSString* for the text, the next arg (font:), arg1, is the font, the last arg, arg2, is highlighted:)

name is generated via A NSString stringWithFormat:@"%@:%@:%.1f:%u". Text is the first arg, [font fontName] is the 2nd, highlighted is the third and [font pointSize] is 4th.

This method not only caches the image for the badge count provided into it, but also returns the image.

Badge Font

On iOS 12-, +[SBIconBadgeView _textFont] is used, but on iOS 13+, -[SBIconBadgeView font] is used. Most of the time, a system font of size 16 is used for badges, but if the SBIconListLayout may specify one.

Other methods

configureForIcon:infoProvider:

This method configures a badge for an icon. You pass in a SBIcon into arg0 and a SBIconView into arg1. It first gets the text / badge count via [arg0 accessoryTextForLocation:[arg1 location]] (getting the text from the location of the SBIconView), and with that generates an image for the badge count via _checkoutImageForText:font:highlighted:. It then calls [self _clearText] to clear any text if there's some already on the badge, and sets the _text and _textImageTuple ivars, as well as sets the image of _textView. The badge will then resize itself for the new text image via _resizeForTextImage, and will update the _displayingAccessory ivar.

prepareForReuse:

Clears _displayingAccessory, _text, and calls [self _clearText]. Then sets nil on the _textImage ivar and calls _resizeForTextImage with nil.

_configureAnimatedForText:highlighted:animator:

Generates the image for the text / badge count, and clears current text / badge count. If the _checkoutImageForText returned nil, the _displayingAccessory is NO, if not, YES. It then will zoom out / zoom in / crossfade the text image depending on the current context.

Continuity Badge Types

(From -[SBIconView continuityBadgeTypeForNSString]) - as of iOS 15.2:

typedef enum {
	SBIconContinuityBadgeTypeNone = 0, /* 0 is also used if the type cannot be found */
	SBIconContinuityBadgeTypePhone = 1,
    SBIconContinuityBadgeTypePad = 2,
	SBIconContinuityBadgeTypePod = 3,
	SBIconContinuityBadgeTypeMac = 4,
	SBIconContinuityBadgeTypeWatch = 5,
	SBIconContinuityBadgeTypeAppleTV = 6,
	SBIconContinuityBadgeTypeAUX = 7,
	SBIconContinuityBadgeTypeBluetooth = 8,
	SBIconContinuityBadgeTypeHeadphones = 9,
    SBIconContinuityBadgeTypeLocation = 10,
    SBIconContinuityBadgeTypeWake = 11,
    SBIconContinuityBadgeTypePhoneX = 12,
    SBIconContinuityBadgeTypeIMac = 13,
    SBIconContinuityBadgeTypeMacBook = 14
} SBIconContinuityBadgeType;