在各种小控件上添加Badge
实现方案:CATextLayer
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(100, 240, 80, 80)];
btn.backgroundColor = [UIColor grayColor];
[self.view addSubview:btn];
_badgeLayer = [[CATextLayer alloc] init];
_badgeLayer.backgroundColor=[UIColor yellowColor].CGColor;
_badgeLayer.foregroundColor = [UIColor blackColor].CGColor;
_badgeLayer.alignmentMode = kCAAlignmentCenter;
[_badgeLayer setFrame:CGRectMake(0, 0, 18, 18)];
_badgeLayer.position=CGPointMake(26, 0);
_badgeLayer.wrapped = YES;
_badgeLayer.cornerRadius = 9.0f;
[_badgeLayer setFontSize:16];
[_badgeLayer setString:@"4"];
_badgeLayer.anchorPoint=CGPointZero;
_badgeLayer.contentsScale = [[UIScreen mainScreen] scale];
[btn.layer addSublayer:_badgeLayer];
本文将指导您如何在iOS应用的各种小控件上添加Badge,通过使用CATextLayer实现这一功能,包括设置颜色、字体大小、位置和样式等关键步骤。
222

被折叠的 条评论
为什么被折叠?



