欢迎光临
我们一直在努力

为任意视图添加圆点和角标文字

建站超值云服务器,限时71元/月

为任意视图添加圆点和角标文字

项目介绍:

关键代码
-(void)yee_MakeBadgeText:(NSString *)text
               textColor:(UIColor *)tColor
               backColor:(UIColor *)backColor
                    Font:(UIFont*)tfont{
    if ([self yee_BadgeLable]==nil) {//如果没有绑定就重新创建,然后绑定
        YeeBadgeLable *badgeLable =[[YeeBadgeLable alloc] init];
        objc_setAssociatedObject(self, YeeBadgeLableString, badgeLable, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        [self addSubview:badgeLable];
    }
    CGSize textSize=[self sizeWithString:text font:tfont constrainedToWidth:self.frame.size.width];
        if ([self isKindOfClass:[UIButton class]]) {
    
            UIButton *weakButton=(UIButton*)self;
            [[self  yee_BadgeLable] makeBrdgeViewWithText:text textColor:tColor backColor:backColor textFont:tfont tframe:CGRectMake(weakButton.imageView.frame.size.width*0.5+weakButton.imageView.frame.origin.x,weakButton.imageView.frame.origin.y, textSize.width+8.0, textSize.height)];
            
        }else if ([self isKindOfClass:[UITabBarItem class]]){
    
    
    
        }else{
    
            [[self  yee_BadgeLable] makeBrdgeViewWithText:text textColor:tColor backColor:backColor textFont:tfont tframe:CGRectMake(self.frame.size.width-(textSize.width+8.0)*0.5, -textSize.height*0.5, textSize.width+8.0, textSize.height)];
        }
}
用法
 UIView *blueView=[[UIView alloc] init];
    
    [blueView setFrame:CGRectMake(40, 70, 200, 20)];
    [blueView  yee_MakeBadgeText:@"" textColor:[UIColor whiteColor] backColor:[UIColor redColor] Font:[UIFont systemFontOfSize:10]];
    [blueView setBackgroundColor:[UIColor blueColor]];
    [self.view addSubview:blueView];
    
    UIView *grayView=[[UIView alloc] init];
    
    [grayView setFrame:CGRectMake(40, 120, 200, 20)];
    [grayView  yee_MakeBadgeText:@"100" textColor:[UIColor whiteColor] backColor:[UIColor redColor] Font:[UIFont systemFontOfSize:10]];
    [grayView setBackgroundColor:[UIColor grayColor]];
    [self.view addSubview:grayView];
    
    
    UIButton *buttonView=[UIButton buttonWithType:UIButtonTypeCustom];
    [buttonView setFrame:CGRectMake(40, 180, 50, 40)];
    [buttonView setImage:[UIImage imageNamed:@"artical_detail_icon_comment_disabled"] forState:UIControlStateNormal];
    [buttonView  yee_MakeBadgeText:@"2345" textColor:[UIColor whiteColor] backColor:[UIColor redColor] Font:[UIFont systemFontOfSize:9]];
    
    [self.view addSubview:buttonView];
  

demo请戳这里,demo会一直更新,如有问题可以pull,希望能帮助到你。

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 为任意视图添加圆点和角标文字
分享到: 更多 (0)