leo*_*nho 19 iphone cocoa-touch
许多iPhone应用程序使用蓝色徽章来指示子视图中的项目数,例如Mail客户端:
iPhoto http://img.skitch.com/20081103-tjr9yupbhgr3sqfh7u56if4rsn.preview.jpg
有没有任何标准方式(甚至API)这样做?
更新:我创建了一个名为BlueBadge的类来执行此操作.它可以在http://github.com/leonho/iphone-libs/tree/master上找到
Ben*_*ieb 22
据我所知,这没有API.但是,使用CoreGraphics(iPhone上没有NSBezierPath),您可以非常轻松地完成.它只是CGPath中的两个弧和一些文本:
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];
Run Code Online (Sandbox Code Playgroud)