Are there any standard way to implement the “blue badge” in iPhone?

后端 未结 5 1283
故里飘歌
故里飘歌 2020-12-22 20:39

A lot of iPhone apps use a blue badge to indicate the number of items in the subviews, such as the Mail client:

iPhoto http://img.skitch.com/20081103-tjr9yupbhgr3sqf

5条回答
  •  长情又很酷
    2020-12-22 21:04

    To my knowledge there's no API for this. However, using CoreGraphics (NSBezierPath is not available on iPhone), you can do it pretty easily. It's just two arcs in a CGPath and some text:

    CGContextRef        context = UIGraphicsGetCurrentContext();
    float               radius = bounds.size.height / 2.0;
    NSString            *countString = [NSString stringWithFormat: @"%d", count];
    
    CGContextClearRect(context, bounds);
    
    CGContextSetFillColorWithColor(context, ovalColor);
    CGContextBeginPath(context);
    CGContextAddArc(context, radius, radius, radius, M_PI / 2 , 3 * M_PI / 2, NO);
    CGContextAddArc(context, bounds.size.width - radius, radius, radius, 3 * M_PI / 2, M_PI / 2, NO);
    CGContextClosePath(context);
    CGContextFillPath(context);
    
    [[UIColor whiteColor] set];
    
    UIFont              *font = [UIFont boldSystemFontOfSize: 14];
    CGSize              numberSize = [countString sizeWithFont: font];
    
    bounds.origin.x = (bounds.size.width - numberSize.width) / 2;
    
    [countString drawInRect: bounds withFont: font];
    

提交回复
热议问题