Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

给 QMUIBadgeProtocol 扩展了qmui_badgeAttributedString 参数.可以使用这个参数.给badge … #1370

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions QMUIKit/QMUIComponents/QMUIBadge/QMUIBadgeProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ NS_ASSUME_NONNULL_BEGIN

/// 用字符串设置未读数,nil 表示不显示未读数
@property(nonatomic, copy, nullable) NSString *qmui_badgeString;
/// 用富文本设置角标.扩展自定义的图片或组合样式
@property(nonatomic, copy, nullable) NSAttributedString *qmui_badgeAttributedString;


@property(nonatomic, strong, nullable) UIColor *qmui_badgeBackgroundColor;
@property(nonatomic, strong, nullable) UIColor *qmui_badgeTextColor;
Expand Down
13 changes: 13 additions & 0 deletions QMUIKit/QMUIComponents/QMUIBadge/UIBarItem+QMUIBadge.m
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,19 @@ - (NSString *)qmui_badgeString {
return (NSString *)objc_getAssociatedObject(self, &kAssociatedObjectKey_badgeString);
}

static char kAssociatedObjectKey_badgeAttributedString;
- (void)setQmui_badgeAttributedString:(NSAttributedString *)qmui_badgeAttributedString{
objc_setAssociatedObject(self, &kAssociatedObjectKey_badgeAttributedString, qmui_badgeAttributedString, OBJC_ASSOCIATION_COPY_NONATOMIC);
if (qmui_badgeAttributedString) {
[self updateViewDidSetBlockIfNeeded];
}
self.qmui_view.qmui_badgeAttributedString = qmui_badgeAttributedString;
}
- (NSAttributedString *)qmui_badgeAttributedString{
return (NSAttributedString *)objc_getAssociatedObject(self, &kAssociatedObjectKey_badgeAttributedString);
}


static char kAssociatedObjectKey_badgeBackgroundColor;
- (void)setQmui_badgeBackgroundColor:(UIColor *)qmui_badgeBackgroundColor {
objc_setAssociatedObject(self, &kAssociatedObjectKey_badgeBackgroundColor, qmui_badgeBackgroundColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
Expand Down
37 changes: 36 additions & 1 deletion QMUIKit/QMUIComponents/QMUIBadge/UIView+QMUIBadge.m
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,41 @@ - (NSString *)qmui_badgeString {
return (NSString *)objc_getAssociatedObject(self, &kAssociatedObjectKey_badgeString);
}

static char kAssociatedObjectKey_badgeAttributedString;
- (void)setQmui_badgeAttributedString:(NSAttributedString *)qmui_badgeAttributedString{
objc_setAssociatedObject(self, &kAssociatedObjectKey_badgeAttributedString, qmui_badgeAttributedString, OBJC_ASSOCIATION_COPY_NONATOMIC);

if (qmui_badgeAttributedString) {
if (!self.qmui_badgeLabel) {
self.qmui_badgeLabel = [[_QMUIBadgeLabel alloc] init];
self.qmui_badgeLabel.clipsToBounds = YES;
self.qmui_badgeLabel.textAlignment = NSTextAlignmentCenter;
self.qmui_badgeLabel.backgroundColor = self.qmui_badgeBackgroundColor;
self.qmui_badgeLabel.font = [UIFont systemFontOfSize:0]; //font 设置为0避免图片周边出现偏移边距.
self.qmui_badgeLabel.contentEdgeInsets = self.qmui_badgeContentEdgeInsets;
self.qmui_badgeLabel.offset = self.qmui_badgeOffset;
self.qmui_badgeLabel.offsetLandscape = self.qmui_badgeOffsetLandscape;
BeginIgnoreDeprecatedWarning
self.qmui_badgeLabel.centerOffset = self.qmui_badgeCenterOffset;
self.qmui_badgeLabel.centerOffsetLandscape = self.qmui_badgeCenterOffsetLandscape;
EndIgnoreDeprecatedWarning
[self addSubview:self.qmui_badgeLabel];

[self updateLayoutSubviewsBlockIfNeeded];
}
self.qmui_badgeLabel.attributedText = qmui_badgeAttributedString;
self.qmui_badgeLabel.hidden = NO;
[self setNeedsUpdateBadgeLabelLayout];
self.clipsToBounds = NO;
}else{
self.qmui_badgeLabel.hidden = YES;
}

}
- (NSAttributedString *)qmui_badgeAttributedString{
return (NSAttributedString *)objc_getAssociatedObject(self, &kAssociatedObjectKey_badgeAttributedString);
}

static char kAssociatedObjectKey_badgeBackgroundColor;
- (void)setQmui_badgeBackgroundColor:(UIColor *)qmui_badgeBackgroundColor {
objc_setAssociatedObject(self, &kAssociatedObjectKey_badgeBackgroundColor, qmui_badgeBackgroundColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
Expand Down Expand Up @@ -252,7 +287,7 @@ - (_QMUIBadgeLabel *)qmui_badgeLabel {
}

- (void)setNeedsUpdateBadgeLabelLayout {
if (self.qmui_badgeString.length) {
if (self.qmui_badgeString.length || self.qmui_badgeAttributedString) {
[self setNeedsLayout];
}
}
Expand Down