我想出了一個簡單的方法來做到這一點,根本不使用任何PNG。我已經子類化一個UIView,然後在drawRect中我這樣做:
- (void)drawRect:(CGRect)rect {
// Drawing code
CGContextRef context = UIGraphicsGetCurrentContext();
[textColor set];
NSString* [email protected]"★★★★★";
rect=self.bounds;
UIFont *font = [UIFont boldSystemFontOfSize: 16];
CGSize starSize = [stars sizeWithFont: font];
rect.size=starSize;
[@"☆☆☆☆☆" drawInRect:rect withFont: font];
CGRect clip=rect;
clip.size.width=clip.size.width*rating/100;
CGContextClipToRect(context,clip);
[stars drawInRect:rect withFont:font];
}
(這些字符串是5空和滿5星,如果他們只顯示我的Mac上我只是內輸入他們IB使用'特殊字符'菜單)
我已經添加了一個textColor(UIColor *)和rating(int)屬性,所以這些是來自哪裏。
要在IB中添加視圖,只需將類類型更改爲我的UIView子類的名稱即可。然後我可以將視圖添加到IB中的任何容器。
+1。好的解決方案 – lostInTransit 2009-06-23 13:40:13