是否有任何控制可用于星级评分?

是否有任何控制可用于iOS中的星级评分? 所以我可以在UITableviewCell中使用?

我知道有一些像DYRating等开放源代码的控制。但iam不能够将其添加到tableview单元格中。

您可以检查ASStar评级。

直接添加控件到你的视图控制器,或者你可以添加到你的单元格。

这里是github链接 。

我推荐HCSStarRatingView 。 它是原生的,它也支持IB_DESIGNABLEIBInspectable

HCSStarRatingView

注意:这是“仅显示”一个。 STARS

我在这里采取了一种不同的方法,我使用带有星号的字体创build了带有归因string的标签,您可以在这里查看我的类别。 我用“SS皮卡”字体,

 #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 

用法:

 self.ratingLabel.attributedText = [NSMutableAttributedString starWithRating:rating outOfTotal:5 withFontSize:9.0f]; 

这是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 } } 

用法:

 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) 

Swift3

https://github.com/marketplacer/Cosmos

  • 安装荚

    target 'TargetName' do

    pod 'Cosmos'

  • 使用此命令安装pod文件来安装pod文件

  • 把一个简单的UIView放在故事板上,并把类名称设置为CosmosView和模块名称Cosmos

在这里输入图像说明

在这里你可以访问属性

在这里输入图像说明

使用https://github.com/hugocampossousa/HCSStarRatingView 。 这很容易实现。 只需写下这些代码行。 在.h文件中为IBOutlet HCSStarRatingView创build一个创build一个string来存储评级值。

 @property (weak, nonatomic) IBOutlet HCSStarRatingView *starRatingView; @property (weak, nonatomic) NSString *ratingStr; 

在.m文件中写下这些行

 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; } } 

是的,有很多库,你可以使用这样的控制,如宇宙宇宙图书馆等级控制

如果你想像这样的东西看起来像评级图(如图中的一个),那么你可以看到这个回购在iOS的 Github 评分图表视图

在这里输入图像说明