ios 是否有任何可用于星级评分的控件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16100378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there any controls available for star rating?
提问by Naveen
Is there any controls available for star rating in ios ? So i can use that in UITableviewCell ??
ios 中是否有可用于星级评定的控件?所以我可以在 UITableviewCell 中使用它??
I know there are some open open source controls like DYRating etc.. But iam not able to add it in tableview cell .
我知道有一些开源控件,如 DYRating 等。但我无法将它添加到 tableview 单元格中。
采纳答案by Naveen
You can check the ASStar rating .
您可以查看 ASStar 评级。
Add the control directly to your view controller or you can add into your cell .
将控件直接添加到您的视图控制器,或者您可以添加到您的单元格中。
And here is github link.
这是 github 链接。
回答by Hlung
I recommend HCSStarRatingView. It is native-looking and it also supports IB_DESIGNABLE
and IBInspectable
.
我推荐HCSStarRatingView。它具有原生外观,并且还支持IB_DESIGNABLE
和IBInspectable
。
回答by Sebastian ?uczak
Take a look, it should suit you just fine:
看一看,它应该很适合你:
https://www.cocoacontrols.com/search?utf8=%E2%9C%93&q=star+rating
https://www.cocoacontrols.com/search?utf8=%E2%9C%93&q=star+rating
回答by anoop4real
Note: This is a "DISPLAY ONLY" one.
I took a different approach here, I created a label with attributed string using a font with stars, you can check my category here. I used "SS Pika" font,
我在这里采用了不同的方法,我使用带星号的字体创建了一个带有属性字符串的标签,您可以在此处查看我的类别。我使用了“SS Pika”字体,
#import "NSMutableAttributedString+Stars.h"
@implementation NSMutableAttributedString (Stars)
+ (NSAttributedString *)starWithRating:(CGFloat)rating
outOfTotal:(NSInteger)totalNumberOfStars
withFontSize:(CGFloat) fontSize{
UIFont *currentFont = [UIFont fontWithName:@"SS Pika" size:fontSize];
NSDictionary *activeStarFormat = @{
NSFontAttributeName : currentFont,
NSForegroundColorAttributeName : [UIColor redColor]
};
NSDictionary *inactiveStarFormat = @{
NSFontAttributeName : currentFont,
NSForegroundColorAttributeName : [UIColor lightGrayColor]
};
NSMutableAttributedString *starString = [NSMutableAttributedString new];
for (int i=0; i < totalNumberOfStars; ++i) {
//Full star
if (rating >= i+1) {
[starString appendAttributedString:[[NSAttributedString alloc]
initWithString:@"\u22C6 " attributes:activeStarFormat]];
}
//Half star
else if (rating > i) {
[starString appendAttributedString:[[NSAttributedString alloc]
initWithString:@"\uE1A1 " attributes:activeStarFormat]];
}
// Grey star
else {
[starString appendAttributedString:[[NSAttributedString alloc]
initWithString:@"\u22C6 " attributes:inactiveStarFormat]];
}
}
return starString;
}
@end
USAGE:
用法:
self.ratingLabel.attributedText = [NSMutableAttributedString starWithRating:rating outOfTotal:5 withFontSize:9.0f];
Here is the SWIFT Version
这是 SWIFT 版本
extension NSMutableAttributedString{
func starWithRating(rating:Float, outOfTotal totalNumberOfStars:NSInteger, withFontSize size:CGFloat) ->NSAttributedString{
let currentFont = UIFont(name: "<USE UR FONT HERE>", size: size)!
let activeStarFormat = [ NSFontAttributeName:currentFont, NSForegroundColorAttributeName: UIColor.redColor()];
let inactiveStarFormat = [ NSFontAttributeName:currentFont, NSForegroundColorAttributeName: UIColor.lightGrayColor()];
let starString = NSMutableAttributedString()
for(var i = 0; i < totalNumberOfStars; ++i){
if(rating >= Float(i+1)){
// This is for selected star. Change the unicode value according to the font that you use
starString.appendAttributedString(NSAttributedString(string: "\u{22C6} ", attributes: activeStarFormat))
}
else if (rating > Float(i)){
// This is for selected star. Change the unicode value according to the font that you use
starString.appendAttributedString(NSAttributedString(string: "\u{E1A1} ", attributes: activeStarFormat))
}
else{
// This is for de-selected star. Change the unicode value according to the font that you use
starString.appendAttributedString(NSAttributedString(string: "\u{22C6} ", attributes: inactiveStarFormat))
}
}
return starString
}
}
USAGE:
用法:
starLabel.attributedText = NSMutableAttributedString().starWithRating(3.5, outOfTotal: 5, withFontSize: 8.0)
starLabelFull.attributedText = NSMutableAttributedString().starWithRating(5, outOfTotal: 5, withFontSize: 8.0)
starLabelZero.attributedText = NSMutableAttributedString().starWithRating(0, outOfTotal: 5, withFontSize: 8.0)
回答by Azharhussain Shaikh
Swift3
Swift3
https://github.com/marketplacer/Cosmos
https://github.com/marketplacer/Cosmos
install pod
target 'TargetName' do
pod 'Cosmos'
Install pod file with this command
pod install
Put a simple UIView in your storyboard and give the class name as
CosmosView
and Module nameCosmos
安装吊舱
target 'TargetName' do
pod 'Cosmos'
使用此命令安装 pod 文件
pod install
在你的故事板中放置一个简单的 UIView 并给出类名
CosmosView
和模块名Cosmos
HERE YOU CAN ACCESS THE ATTRIBUTES
在这里您可以访问属性
回答by user1113101
You can check following controls from Cocoacontrols.
您可以从 Cocoacontrols 中查看以下控件。
https://www.cocoacontrols.com/controls/star-rating-view
https://www.cocoacontrols.com/controls/star-rating-view
https://www.cocoacontrols.com/controls/scrratingview
https://www.cocoacontrols.com/controls/scrratingview
回答by Leszek Szary
I just wrote this simple UIView subclass that draws stars rating from 0 to 5 including half stars. You can set the color with tintColor property and set the rating in interface editor. View width should be set to 5 * height.
我刚刚编写了这个简单的 UIView 子类,它绘制了从 0 到 5 的星级,包括半颗星。您可以使用 tintColor 属性设置颜色并在界面编辑器中设置评级。视图宽度应设置为 5 * 高度。
class StarsView: UIView {
@IBInspectable var rating: Double = 5.0 {
didSet {
setNeedsLayout()
}
}
func starPath(size: CGFloat, full: Bool) -> UIBezierPath {
let fullPoints = [CGPoint(x: 0.5, y: 0.03), CGPoint(x: 0.61, y: 0.38), CGPoint(x: 0.99, y: 0.38), CGPoint(x: 0.68, y: 0.61), CGPoint(x: 0.8, y: 0.97), CGPoint(x: 0.5, y: 0.75), CGPoint(x: 0.2, y: 0.97), CGPoint(x: 0.32, y: 0.61), CGPoint(x: 0.01, y: 0.38), CGPoint(x: 0.39, y: 0.38)].map({ CGPoint(x: @property (weak, nonatomic) IBOutlet HCSStarRatingView *starRatingView;
@property (weak, nonatomic) NSString *ratingStr;
.x * size, y: self.starRatingView.maximumValue = 5;
self.starRatingView.minimumValue = 0;
self.starRatingView.value = 0;
self.starRatingView.tintColor = [UIColor yellowColor];
self.starRatingView.allowsHalfStars = YES;
self.starRatingView.emptyStarImage = [[UIImage imageNamed:@"heart-empty"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
self.starRatingView.filledStarImage = [[UIImage imageNamed:@"heart-full"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[self.starRatingView addTarget:self action:@selector(didChangeValue:) forControlEvents:UIControlEventValueChanged];
- (IBAction)didChangeValue:(HCSStarRatingView *)sender {
NSLog(@"Changed rating to %.1f", sender.value);
ratingStr = [NSString stringWithFormat:@"%.1f",sender.value];
if([hostRatingStr isEqualToString:@"-0.0"]){
self.ratingLbl.text = @"0.0";
}else{
self.ratingLbl.text = hostRatingStr;
}
}
.y * size) })
let halfPoints = [CGPoint(x: 0.5, y: 0.03), CGPoint(x: 0.5, y: 0.75), CGPoint(x: 0.2, y: 0.97), CGPoint(x: 0.32, y: 0.61), CGPoint(x: 0.01, y: 0.38), CGPoint(x: 0.39, y: 0.38)].map({ CGPoint(x: let ratingsDisplay = [
"★☆☆☆☆",
"★★☆☆☆",
"★★★☆☆",
"★★★★☆",
"★★★★★"
]
let rating = 4
let label = UILabel()
label.text = ratingsDisplay[rating - 1]
.x * size, y: ##代码##.y * size) })
let points = full ? fullPoints : halfPoints
let starPath = UIBezierPath()
starPath.move(to: points.last!)
for point in points {
starPath.addLine(to: point)
}
return starPath
}
func starLayer(full: Bool) -> CAShapeLayer {
let shapeLayer = CAShapeLayer()
shapeLayer.path = starPath(size: bounds.size.height, full: full).cgPath
shapeLayer.fillColor = tintColor.cgColor
return shapeLayer
}
override func layoutSubviews() {
super.layoutSubviews()
let sublayers = layer.sublayers
for sublayer in sublayers ?? [] {
sublayer.removeFromSuperlayer()
}
for i in 1...5 {
if rating >= Double(i) - 0.5 {
let star = starLayer(full: rating >= Double(i))
star.transform = CATransform3DMakeTranslation(bounds.size.height * CGFloat(i - 1), 0, 0)
layer.addSublayer(star)
}
}
}
}
回答by User558
Use https://github.com/hugocampossousa/HCSStarRatingView. it easy to implement this. just write down these lines of code. Create IBOutlet HCSStarRatingView for in .h file an create one string for storing the rating value.
使用https://github.com/hugocampossousa/HCSStarRatingView。很容易实现这一点。只需写下这些代码行。在 .h 文件中为 IBOutlet HCSStarRatingView 创建一个用于存储评级值的字符串。
##代码##in .m file write down these lines
在 .m 文件中写下这些行
##代码##回答by Nikesh Jha
Yeah there are many libraries which you can use to make such controls like cosmos Cosmos library for rating control
是的,您可以使用许多库来制作此类控件,例如用于评级控制的 Cosmos Cosmos 库
if you want something like that looks like rating graph (like the one in image)then you can see this repo in github Rating graph view in iOS
回答by closetCoder
If all you want is whole numbers of stars, you can use a simple String Array and use it to set the text property of a UILabel.
如果你想要的是整数个星星,你可以使用一个简单的字符串数组并用它来设置 UILabel 的文本属性。
##代码##Make sure you set the font of the UILabel to one which displays the black and white stars at the same size. Many but not all do.
确保将 UILabel 的字体设置为以相同大小显示黑白星星的字体。许多但不是全部。