http://blog.csdn.net/fanjunxi1990/article/details/8663914

由于项目需求,需要做一个列表,里面有各个商品的评分,就是app store里面所有app的星级评分

下面是DisplayStarView.h

  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;        /* 根据字体大小来确定星星的大小 */
  12. NSInteger _maxStar;      /* 总共的长度 */
  13. NSInteger _showStar;    //需要显示的星星的长度
  14. UIColor *_emptyColor;   //未点亮时候的颜色
  15. UIColor *_fullColor;    //点亮的星星的颜色
  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如下

  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. //默认的星星的大小是 13.0f
  22. self.starSize = 13.0f;
  23. //未点亮时的颜色是 灰色的
  24. self.emptyColor = [UIColor colorWithRed:167.0f / 255.0f green:167.0f / 255.0f blue:167.0f / 255.0f alpha:1.0f];
  25. //点亮时的颜色是 亮黄色的
  26. self.fullColor = [UIColor colorWithRed:255.0f / 255.0f green:121.0f / 255.0f blue:22.0f / 255.0f alpha:1.0f];
  27. //默认的长度设置为100
  28. self.maxStar = 100;
  29. }
  30. return self;
  31. }
  32. //重绘视图
  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

需要怎么去使用呢?很简单,值需要知道评分是多少就OK啦,比如

  1. //评论是4.2分的
  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. //评论是2.5分的
  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. //评论是4.8分的
  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];

运行结果如图所示:

 

最新文章

  1. 移动端上传图片iphone图片旋转以及服务端处理方法
  2. C#查看各种变量的指针地址
  3. Xcode打印frame id
  4. Hadoop学习17--yarn配置篇-内存管理
  5. NeHe OpenGL教程 第二十六课:反射
  6. Netty那点事
  7. RAC 数据库的启动与关闭
  8. C语言综述
  9. SecureCRT 7.3.4 安装以及破解
  10. Linux常用系统调用
  11. C#如何以管理员身份运行程序(转)
  12. 在asp.net中导出表格Excel数据
  13. 项目实战2.2—nginx 反向代理负载均衡、动静分离和缓存的实现
  14. 第一章:Python基础の快速认识基本语法
  15. javascript中的Promise使用
  16. Pat1071: Speech Patterns
  17. JS中的continue,break,return的区别
  18. 【C++】读取参数的类
  19. 使用Google cardboard 2的一些软件
  20. Secure Shell相关设置

热门文章

  1. 19-10-16-R
  2. 工控安全入门(七)—— plc的网络
  3. MySQL实战总结
  4. 常见Idea插件
  5. sulin LuceneNet 搜索二
  6. Sublime setting 个性修改
  7. img标签中的onerror事件
  8. jeecms 基本架构研究
  9. nginx链接末尾自动补全斜杠
  10. [jnhs]全套CRC校验 算法