How to show a star rating on iphone

Source: Internet
Author: User

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
  1. //
  2. DisplayStarView.h
  3. Testexpress
  4. //
  5. Created by Juncy_fan on 13-3-12.
  6. Copyright (c) 2013 Juncy_fan. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. @Interface Displaystarview:uiview
  10. {
  11. CGFloat _starsize; / * Determine the size of the star according to the font size * /
  12. Nsinteger _maxstar; / * Total length * /
  13. Nsinteger _showstar; //need to show the length of the star
  14. Uicolor *_emptycolor; //color when not lit
  15. Uicolor *_fullcolor; //Light up the colors of the stars
  16. }
  17. @property (nonatomic, assign) CGFloat starsize;
  18. @property (nonatomic, assign) Nsinteger Maxstar;
  19. @property (nonatomic, assign) Nsinteger Showstar;
  20. @property (nonatomic, retain) Uicolor *emptycolor;
  21. @property (nonatomic, retain) Uicolor *fullcolor;
  22. @end


DISPLAYSTARVIEW.M as follows

[CSharp]View Plaincopy
  1. //
  2. displaystarview.m
  3. Testexpress
  4. //
  5. Created by Juncy_fan on 13-3-12.
  6. Copyright (c) 2013 Juncy_fan. All rights reserved.
  7. //
  8. #import "DisplayStarView.h"
  9. @implementation Displaystarview
  10. @synthesize starsize = _starsize;
  11. @synthesize Maxstar = _maxstar;
  12. @synthesize Showstar = _showstar;
  13. @synthesize emptycolor = _emptycolor;
  14. @synthesize fullcolor = _fullcolor;
  15. -(ID) initWithFrame: (CGRect) frame
  16. {
  17. self = [super Initwithframe:frame];
  18. if (self)
  19. {
  20. Self.backgroundcolor = [Uicolor Clearcolor];
  21. //The size of the default star is 13.0f
  22. Self.starsize = 13.0f;
  23. //The color is gray when not lit
  24. Self.emptycolor = [Uicolor colorwithred:167.0f/255.0f green:167.0f/255.0f blue:167.0f/255.0f alpha:1.0f];
  25. //The color is bright yellow when lit
  26. Self.fullcolor = [Uicolor colorwithred:255.0f/255.0f green:121.0f/255.0f blue:22.0f/255.0f alpha:1.0f];
  27. //The default length is set to
  28. Self.maxstar = 100;
  29. }
  30. return self;
  31. }
  32. Redraw a View
  33. -(void) DrawRect: (cgrect) Rect
  34. {
  35. //Drawing code
  36. Cgcontextref context = Uigraphicsgetcurrentcontext ();
  37. nsstring* stars = @"★★★★★";
  38. rect = self.bounds;
  39. Uifont *font = [Uifont boldsystemfontofsize:_starsize];
  40. Cgsize starsize = [stars Sizewithfont:font];
  41. Rect.size=starsize;
  42. [_emptycolor set];
  43. [stars Drawinrect:rect Withfont:font];
  44. CGRect clip = rect;
  45. Clip.size.width = Clip.size.width * _showstar/_maxstar;
  46. Cgcontextcliptorect (Context,clip);
  47. [_fullcolor set];
  48. [stars Drawinrect:rect Withfont:font];
  49. }
  50. -(void) Dealloc
  51. {
  52. [_emptycolor release];
  53. [_fullcolor release];
  54. [Super Dealloc];
  55. }
  56. @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
  1. The review is 4.2 minutes.
  2. Displaystarview *SV = [[Displaystarview alloc]initwithframe:cgrectmake (90, 90, 200, 40)];
  3. [Self.view ADDSUBVIEW:SV];
  4. Sv.showstar = 4.2*20;
  5. [SV release];
  6. //Reviews are 2.5 points
  7. Displaystarview *SV1 = [[Displaystarview alloc]initwithframe:cgrectmake (90, 90+40, 200, 40)];
  8. [Self.view ADDSUBVIEW:SV1];
  9. Sv1.showstar = 2.5 * 20;
  10. [SV1 release];
  11. //Reviews are 4.8 points
  12. Displaystarview *sv2 = [[Displaystarview alloc]initwithframe:cgrectmake (90, 90+40+40, 200, 40)];
  13. [Self.view Addsubview:sv2];
  14. Sv2.showstar = 4.8 * 20;
  15. [Sv2 release];


Operation Result:

How to show a star rating on iphone

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.