如何在iPhone 显示一个 星级评分
2014-02-15 21:18
302 查看
由于项目需求,需要做一个列表,里面有各个商品的评分,就是app store里面所有app的星级评分
下面是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; /* 根据字体大小来确定星星的大小 */
NSInteger _maxStar; /* 总共的长度 */
NSInteger _showStar; //需要显示的星星的长度
UIColor *_emptyColor; //未点亮时候的颜色
UIColor *_fullColor; //点亮的星星的颜色
}
@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如下
[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];
//默认的星星的大小是 13.0f
self.starSize = 13.0f;
//未点亮时的颜色是 灰色的
self.emptyColor = [UIColor colorWithRed:167.0f / 255.0f green:167.0f / 255.0f blue:167.0f / 255.0f alpha:1.0f];
//点亮时的颜色是 亮黄色的
self.fullColor = [UIColor colorWithRed:255.0f / 255.0f green:121.0f / 255.0f blue:22.0f / 255.0f alpha:1.0f];
//默认的长度设置为100
self.maxStar = 100;
}
return self;
}
//重绘视图
- (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
需要怎么去使用呢?很简单,值需要知道评分是多少就OK啦,比如
[csharp] view
plaincopy
//评论是4.2分的
DisplayStarView *sv = [[DisplayStarView alloc]initWithFrame:CGRectMake(90, 90, 200, 40)];
[self.view addSubview:sv];
sv.showStar = 4.2*20;
[sv release];
//评论是2.5分的
DisplayStarView *sv1 = [[DisplayStarView alloc]initWithFrame:CGRectMake(90, 90+40, 200, 40)];
[self.view addSubview:sv1];
sv1.showStar = 2.5 * 20;
[sv1 release];
//评论是4.8分的
DisplayStarView *sv2 = [[DisplayStarView alloc]initWithFrame:CGRectMake(90, 90+40+40, 200, 40)];
[self.view addSubview:sv2];
sv2.showStar = 4.8 * 20;
[sv2 release];
运行结果如图所示:
下面是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; /* 根据字体大小来确定星星的大小 */
NSInteger _maxStar; /* 总共的长度 */
NSInteger _showStar; //需要显示的星星的长度
UIColor *_emptyColor; //未点亮时候的颜色
UIColor *_fullColor; //点亮的星星的颜色
}
@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如下
[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];
//默认的星星的大小是 13.0f
self.starSize = 13.0f;
//未点亮时的颜色是 灰色的
self.emptyColor = [UIColor colorWithRed:167.0f / 255.0f green:167.0f / 255.0f blue:167.0f / 255.0f alpha:1.0f];
//点亮时的颜色是 亮黄色的
self.fullColor = [UIColor colorWithRed:255.0f / 255.0f green:121.0f / 255.0f blue:22.0f / 255.0f alpha:1.0f];
//默认的长度设置为100
self.maxStar = 100;
}
return self;
}
//重绘视图
- (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
需要怎么去使用呢?很简单,值需要知道评分是多少就OK啦,比如
[csharp] view
plaincopy
//评论是4.2分的
DisplayStarView *sv = [[DisplayStarView alloc]initWithFrame:CGRectMake(90, 90, 200, 40)];
[self.view addSubview:sv];
sv.showStar = 4.2*20;
[sv release];
//评论是2.5分的
DisplayStarView *sv1 = [[DisplayStarView alloc]initWithFrame:CGRectMake(90, 90+40, 200, 40)];
[self.view addSubview:sv1];
sv1.showStar = 2.5 * 20;
[sv1 release];
//评论是4.8分的
DisplayStarView *sv2 = [[DisplayStarView alloc]initWithFrame:CGRectMake(90, 90+40+40, 200, 40)];
[self.view addSubview:sv2];
sv2.showStar = 4.8 * 20;
[sv2 release];
运行结果如图所示:
![](http://img.my.csdn.net/uploads/201303/12/1363073691_9812.png)
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- js判断客户端是iOS还是Android等移动终端的方法
- IOS开发环境windows化攻略
- 探讨Android与iOS,我们将何去何从?
- IOS 身份证校验详细介绍及示例代码
- iOS NSDate中关于夏令时的坑
- iOS内存错误EXC_BAD_ACCESS的解决方法
- 从 Auto Layout 的布局算法谈性能
- 浅谈 MVC、MVP 和 MVVM 架构模式
- 深入解析 ObjC 中方法的结构
- 你真的了解 load 方法么?
- 从源代码看 ObjC 中消息的发送
- vm安装mac os x 10.7 lion
- objective-c(一)
- objective-c(三)
- objective-c(四)-基本数据类型以及循环和选择结构
- objective-c(六)文件分离,合成存取方法,方法多参数,局部变量,self使用(未完成)