本文介绍了是否有任何标准方法来实施“蓝色徽章”在iPhone?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很多iPhone应用程式使用蓝色标志来表示子视窗中的项目数量,例如邮件用户端:

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

有没有任何标准方法(甚至是API)这样做?

Are there any standards way (or even an API) do this?

创建了一个类BlueBadge来做到这一点。您可以访问

UPDATE: I have created a class called BlueBadge to do this. It is available at http://github.com/leonho/iphone-libs/tree/master

推荐答案

据我所知,这里没有API。但是,使用CoreGraphics(NSBezierPath不可用在iPhone上),你可以很容易做到。它只是一个CGPath和一些文本中的两个弧:

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];

这篇关于是否有任何标准方法来实施“蓝色徽章”在iPhone?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 00:24