19

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

我知道有一些开源控件,例如 DYRating 等。但是我无法将它添加到 tableview 单元格中。

4

11 回答 11

19

我推荐HCSStarRatingView。它具有原生外观,还支持IB_DESIGNABLEIBInspectable.

HCSStarRatingView

于 2015-08-18T04:27:32.677 回答
13

您可以查看 ASStar 评级。

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

这是 github链接

于 2013-04-27T11:15:34.150 回答
8

看看,它应该适合你:

https://www.cocoacontrols.com/search?utf8=%E2%9C%93&q=star+rating

于 2013-04-19T08:37:10.503 回答
5

注意:这是一个“仅显示”的。 明星

我在这里采用了不同的方法,我使用带有星号的字体创建了一个带有属性字符串的标签,您可以在此处查看我的类别。我用了“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

用法:

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)
于 2016-08-06T20:59:30.950 回答
5

Swift3

https://github.com/marketplacer/Cosmos

  • 安装吊舱

    target 'TargetName' do

    pod 'Cosmos'

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

  • 在你的故事板中放一个简单的 UIView 并给出类名CosmosView和模块名Cosmos

在此处输入图像描述

在这里您可以访问属性

在此处输入图像描述

于 2017-05-05T11:57:24.877 回答
3

您可以从 Cocoacontrols 检查以下控件。

https://www.cocoacontrols.com/controls/star-rating-view

https://www.cocoacontrols.com/controls/scrratingview

https://www.cocoacontrols.com/controls/amratingcontrol

于 2013-04-19T09:16:46.080 回答
3

我刚刚编写了这个简单的 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: $0.x * size, y: $0.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: $0.x * size, y: $0.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)
            }
        }
    }
}
于 2018-07-15T20:04:07.427 回答
2

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

@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;
   }
 }
于 2017-05-29T10:32:17.347 回答
2

使用https://github.com/shantaramk/RatingView

实现这一点毫不费力。只需按照以下步骤操作:

设置星景IBOutlet

    @IBOutlet weak var rateView: StarRateView!

准备星级视图

func setupStarRateView() {
    self.rateView.delegate = self
    self.rateView.maxCount = 5
    self.rateView.fillImage = UIImage(named: "fill_star")!
    self.rateView.emptyImage = UIImage(named: "empty_star")!
}

采用率查看代表

extension RateServiceViewController: RatingViewDelegate {

func updateRatingFormatValue(_ value: Int) {
    print("Rating : = ", value)
    }
}
于 2019-01-11T05:56:26.027 回答
1

是的,您可以使用许多库来制作此类控件,例如用于评级控制的 cosmos Cosmos 库

如果你想要看起来像评级图的东西(如图片中的那个),那么你可以在 iOS 的 github 评级图视图中看到这个 repo

在此处输入图像描述

于 2017-11-30T11:04:37.027 回答
1

如果您想要的只是整数个星,您可以使用一个简单的字符串数组并使用它来设置 UILabel 的 text 属性。

let ratingsDisplay = [
    "★☆☆☆☆",
    "★★☆☆☆",
    "★★★☆☆",
    "★★★★☆",
    "★★★★★"
]

let rating = 4
let label = UILabel()
label.text = ratingsDisplay[rating - 1]

确保将 UILabel 的字体设置为以相同大小显示黑白星星的字体。许多但不是全部都这样做。

于 2018-06-03T17:06:18.067 回答