zoukankan      html  css  js  c++  java
  • 如何在iPhone 显示一个 星级评分

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

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

    下面是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.  
    9. #import <Foundation/Foundation.h>  
    10.   
    11. @interface DisplayStarView : UIView  
    12. {  
    13.     CGFloat _starSize;        /* 根据字体大小来确定星星的大小 */  
    14.     NSInteger _maxStar;      /* 总共的长度 */  
    15.     NSInteger _showStar;    //需要显示的星星的长度  
    16.     UIColor *_emptyColor;   //未点亮时候的颜色  
    17.     UIColor *_fullColor;    //点亮的星星的颜色  
    18. }  
    19. @property (nonatomic, assign) CGFloat starSize;  
    20. @property (nonatomic, assign) NSInteger maxStar;  
    21. @property (nonatomic, assign) NSInteger showStar;  
    22. @property (nonatomic, retain) UIColor *emptyColor;  
    23. @property (nonatomic, retain) UIColor *fullColor;  
    24. @end  


    DisplayStarView.m如下

    [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.  
    9. #import "DisplayStarView.h"  
    10.   
    11. @implementation DisplayStarView  
    12. @synthesize starSize = _starSize;  
    13. @synthesize maxStar = _maxStar;  
    14. @synthesize showStar = _showStar;  
    15. @synthesize emptyColor = _emptyColor;  
    16. @synthesize fullColor = _fullColor;  
    17.   
    18. - (id)initWithFrame:(CGRect)frame  
    19. {  
    20.     self = [super initWithFrame:frame];  
    21.     if (self)  
    22.     {  
    23.         self.backgroundColor = [UIColor clearColor];  
    24.         //默认的星星的大小是 13.0f  
    25.         self.starSize = 13.0f;  
    26.         //未点亮时的颜色是 灰色的  
    27.         self.emptyColor = [UIColor colorWithRed:167.0f / 255.0f green:167.0f / 255.0f blue:167.0f / 255.0f alpha:1.0f];  
    28.         //点亮时的颜色是 亮黄色的  
    29.         self.fullColor = [UIColor colorWithRed:255.0f / 255.0f green:121.0f / 255.0f blue:22.0f / 255.0f alpha:1.0f];  
    30.         //默认的长度设置为100  
    31.         self.maxStar = 100;  
    32.     }  
    33.       
    34.     return self;  
    35. }  
    36.   
    37. //重绘视图  
    38. - (void)drawRect:(CGRect)rect  
    39. {  
    40.     // Drawing code  
    41.     CGContextRef context = UIGraphicsGetCurrentContext();  
    42.       
    43.     NSString* stars = @"★★★★★";  
    44.       
    45.     rect = self.bounds;  
    46.     UIFont *font = [UIFont boldSystemFontOfSize:_starSize];  
    47.     CGSize starSize = [stars sizeWithFont:font];  
    48.     rect.size=starSize;  
    49.     [_emptyColor set];  
    50.     [stars drawInRect:rect withFont:font];  
    51.       
    52.     CGRect clip = rect;  
    53.     clip.size.width = clip.size.width * _showStar / _maxStar;  
    54.     CGContextClipToRect(context,clip);  
    55.     [_fullColor set];  
    56.     [stars drawInRect:rect withFont:font];  
    57. }  
    58.   
    59. - (void)dealloc  
    60. {  
    61.     [_emptyColor release];  
    62.     [_fullColor release];  
    63.       
    64.     [super dealloc];  
    65. }  
    66.   
    67. @end  


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

    [csharp] view plaincopy
    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.       
    7.     //评论是2.5分的  
    8.     DisplayStarView *sv1 = [[DisplayStarView alloc]initWithFrame:CGRectMake(90, 90+40, 200, 40)];  
    9.     [self.view addSubview:sv1];  
    10.     sv1.showStar = 2.5 * 20;  
    11.     [sv1 release];  
    12.       
    13.     //评论是4.8分的  
    14.     DisplayStarView *sv2 = [[DisplayStarView alloc]initWithFrame:CGRectMake(90, 90+40+40, 200, 40)];  
    15.     [self.view addSubview:sv2];  
    16.     sv2.showStar = 4.8 * 20;  
    17.     [sv2 release];  


    运行结果如图所示:

     
  • 相关阅读:
    Nginx 重定向 伪静态 rewrite index.php
    centos安装lnmp
    spring @RequestBody 和 @RequestParams 同时使用
    idea 设置加载多个资源文件,显示本地图片
    阿里云栖社区dubbo 资源整理
    idea npm 调试报错解决办法
    git 设置了ssh key 还是需要输入账户和密码
    zookeeper系列 (第一章 :ubuntu 下安装zookeeper)
    linux 下node升级
    解决spring http输入流和输出流只能读取一次
  • 原文地址:https://www.cnblogs.com/itlover2013/p/4855127.html
Copyright © 2011-2022 走看看