http://blog.csdn.net/fanjunxi1990/article/details/8663914
As a result of the project needs, a list of items, which has a rating of each item, is the star rating of all apps in the App Store
Here's DisplayStarView.h.
[CSharp]View Plaincopy
- //
- DisplayStarView.h
- Testexpress
- //
- Created by Juncy_fan on 13-3-12.
- Copyright (c) 2013 Juncy_fan. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- @Interface Displaystarview:uiview
- {
- CGFloat _starsize; / * Determine the size of the star according to the font size * /
- Nsinteger _maxstar; / * Total length * /
- Nsinteger _showstar; //need to show the length of the star
- Uicolor *_emptycolor; //color when not lit
- Uicolor *_fullcolor; //Light up the colors of the stars
- }
- @property (nonatomic, assign) CGFloat starsize;
- @property (nonatomic, assign) Nsinteger Maxstar;
- @property (nonatomic, assign) Nsinteger Showstar;
- @property (nonatomic, retain) Uicolor *emptycolor;
- @property (nonatomic, retain) Uicolor *fullcolor;
- @end
DISPLAYSTARVIEW.M as follows
[CSharp]View Plaincopy
- //
- displaystarview.m
- Testexpress
- //
- Created by Juncy_fan on 13-3-12.
- Copyright (c) 2013 Juncy_fan. All rights reserved.
- //
- #import "DisplayStarView.h"
- @implementation Displaystarview
- @synthesize starsize = _starsize;
- @synthesize Maxstar = _maxstar;
- @synthesize Showstar = _showstar;
- @synthesize emptycolor = _emptycolor;
- @synthesize fullcolor = _fullcolor;
- -(ID) initWithFrame: (CGRect) frame
- {
- self = [super Initwithframe:frame];
- if (self)
- {
- Self.backgroundcolor = [Uicolor Clearcolor];
- //The size of the default star is 13.0f
- Self.starsize = 13.0f;
- //The color is gray when not lit
- Self.emptycolor = [Uicolor colorwithred:167.0f/255.0f green:167.0f/255.0f blue:167.0f/255.0f alpha:1.0f];
- //The color is bright yellow when lit
- Self.fullcolor = [Uicolor colorwithred:255.0f/255.0f green:121.0f/255.0f blue:22.0f/255.0f alpha:1.0f];
- //The default length is set to
- Self.maxstar = 100;
- }
- return self;
- }
- Redraw a View
- -(void) DrawRect: (cgrect) Rect
- {
- //Drawing code
- Cgcontextref context = Uigraphicsgetcurrentcontext ();
- nsstring* stars = @"★★★★★";
- rect = self.bounds;
- Uifont *font = [Uifont boldsystemfontofsize:_starsize];
- Cgsize starsize = [stars Sizewithfont:font];
- Rect.size=starsize;
- [_emptycolor set];
- [stars Drawinrect:rect Withfont:font];
- CGRect clip = rect;
- Clip.size.width = Clip.size.width * _showstar/_maxstar;
- Cgcontextcliptorect (Context,clip);
- [_fullcolor set];
- [stars Drawinrect:rect Withfont:font];
- }
- -(void) Dealloc
- {
- [_emptycolor release];
- [_fullcolor release];
- [Super Dealloc];
- }
- @end
How do I need to use it? It's simple, the value needs to know what the score is OK, like
[CSharp]View Plaincopy
- The review is 4.2 minutes.
- Displaystarview *SV = [[Displaystarview alloc]initwithframe:cgrectmake (90, 90, 200, 40)];
- [Self.view ADDSUBVIEW:SV];
- Sv.showstar = 4.2*20;
- [SV release];
- //Reviews are 2.5 points
- Displaystarview *SV1 = [[Displaystarview alloc]initwithframe:cgrectmake (90, 90+40, 200, 40)];
- [Self.view ADDSUBVIEW:SV1];
- Sv1.showstar = 2.5 * 20;
- [SV1 release];
- //Reviews are 4.8 points
- Displaystarview *sv2 = [[Displaystarview alloc]initwithframe:cgrectmake (90, 90+40+40, 200, 40)];
- [Self.view Addsubview:sv2];
- Sv2.showstar = 4.8 * 20;
- [Sv2 release];
Operation Result:
How to show a star rating on iphone