【问题标题】:Is there any controls available for star rating?是否有任何可用于星级评定的控件?
【发布时间】:2013-04-12 14:18:56
【问题描述】:

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

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

【问题讨论】:

  • Bhargavi:但我需要在 uitableviewcell 中添加它,以便每一行我都可以输入该星号,然后它将显示与评级对应的星号
  • 您尝试过创建 CustomCells 吗?
  • 我没听懂你?你能解释一下吗?

标签: ios objective-c rating fractions


【解决方案1】:

【讨论】:

  • 在tableviewcell里面很好但是很难实现
【解决方案2】:
【解决方案3】:

您可以查看 ASStar 评级。

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

这里是github link

【讨论】:

  • 如何获得星数,,,,,,,,
【解决方案4】:

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

【讨论】:

    【解决方案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)
    

    【讨论】:

      【解决方案6】:

      Swift3

      https://github.com/marketplacer/Cosmos

      • 安装 pod

        target 'TargetName' do

        pod 'Cosmos'

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

      • 在故事板中放置一个简单的 UIView,并将类名称指定为 CosmosView 和模块名称 Cosmos

      您可以在此处访问属性

      【讨论】:

        【解决方案7】:

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

        【讨论】:

          【解决方案8】:

          是的,有很多库可以用来制作像 cosmos 这样的控件 Cosmos library for rating control

          如果你想要看起来像评级图的东西(如图片中的那个),那么你可以看到 这个仓库在 github Rating graph view in iOS

          【讨论】:

          • 我可以在目标 c 中得到这个吗?
          • 真的很有帮助。
          【解决方案9】:

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

          let ratingsDisplay = [
              "★☆☆☆☆",
              "★★☆☆☆",
              "★★★☆☆",
              "★★★★☆",
              "★★★★★"
          ]
          
          let rating = 4
          let label = UILabel()
          label.text = ratingsDisplay[rating - 1]
          

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

          【讨论】:

            【解决方案10】:

            我刚刚编写了这个简单的 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)
                        }
                    }
                }
            }
            

            【讨论】:

              【解决方案11】:

              使用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)
                  }
              }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2012-01-20
                • 1970-01-01
                • 2023-03-04
                • 1970-01-01
                • 1970-01-01
                • 2022-06-29
                • 1970-01-01
                • 2021-05-31
                相关资源
                最近更新 更多