2013-04-19 36 views
14

Có bất kỳ điều khiển nào có sẵn cho xếp hạng sao trong ios không? Vì vậy, tôi có thể sử dụng trong UITableviewCell ??Có bất kỳ điều khiển nào có sẵn cho xếp hạng sao không?

Tôi biết có một số điều khiển nguồn mở như DYRating vv .. Nhưng iam không thể thêm nó vào ô xem bảng.

+0

Nhìn vào http://www.raywenderlich.com/1768/how-to-make-a-custom-uiview-a-5-star-rating-view –

+0

Bhargavi: nhưng tôi cần phải thêm điều đó vào uitableviewcell vì vậy mỗi hàng tôi có thể đưa đầu vào cho ngôi sao đó, sau đó nó sẽ hiển thị ngôi sao tương ứng với xếp hạng – Naveen

+0

Bạn đã thử với việc tạo CustomCells chưa? –

Trả lời

11

Bạn có thể kiểm tra đánh giá ASStar.

Thêm điều khiển trực tiếp vào bộ điều khiển chế độ xem của bạn hoặc bạn có thể thêm vào ô của mình.

Và đây là github link.

+0

cách nhận số sao ,,,,,, –

17

Tôi khuyên bạn nên HCSStarRatingView. Nó có nguồn gốc tự nhiên và nó cũng hỗ trợ IB_DESIGNABLEIBInspectable.

HCSStarRatingView

+0

Cảm ơn bạn đã kiểm soát xếp hạng sao cho IBDesignable –

3

Lưu ý: Đây là "CHỈ DISPLAY". STARS

Tôi đã tiếp cận khác ở đây, tôi đã tạo nhãn có chuỗi được phân bổ bằng phông chữ có dấu sao, bạn có thể kiểm tra danh mục của tôi tại đây.Tôi đã từng "SS Pika" font,

#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 

SỬ DỤNG:

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

Đây là phiên bản 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 
    } 
} 

SỬ DỤNG:

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) 
1

Swift3

https://github.com/marketplacer/Cosmos

  • cài đặt pod

    target 'TargetName' do

    pod 'Cosmos'

  • Cài đặt tập tin pod với lệnh này pod install

  • Đặt một UIView đơn giản trong kịch bản của bạn và cung cấp cho các tên lớp như CosmosView và tên module Cosmos

enter image description here

ĐÂY BẠN CÓ THỂ TRUY CẬP các thuộc tính

enter image description here

0

Sử dụng https://github.com/hugocampossousa/HCSStarRatingView. thật dễ dàng để thực hiện điều này. chỉ cần viết ra những dòng mã này. Tạo IBOutlet HCSStarRatingView cho tệp .h tạo một chuỗi để lưu trữ giá trị xếp hạng.

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

trong file .m viết ra những dòng nà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; 
    } 
} 
-2

Vâng có rất nhiều thư viện mà bạn có thể sử dụng để làm điều khiển như vậy giống như vũ trụ Cosmos library for rating control

nếu bạn muốn một cái gì đó như thế trông giống như biểu đồ xếp hạng (như hình ảnh trong hình ảnh) thì bạn có thể thấy repo này trong github Rating graph view in iOS

enter image description here

+0

Tôi không hiểu rõ điều này..Tôi làm việc tốt !!!! –

Các vấn đề liên quan