ios中是否有可用于星级的控件?所以我可以在UITableviewCell中使用它?
我知道有一些开放的开源控件,比如DYRating等。但是我无法在tableview单元格中添加它。
答案 0 :(得分:18)
我建议HCSStarRatingView。它具有原生外观,并且还支持IB_DESIGNABLE
和IBInspectable
。
答案 1 :(得分:12)
答案 2 :(得分:8)
答案 3 :(得分: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)
答案 4 :(得分:3)
您可以查看Cocoacontrols的以下控件。
https://www.cocoacontrols.com/controls/star-rating-view
答案 5 :(得分: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;
}
}
答案 6 :(得分:2)
我刚刚编写了这个简单的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)
}
}
}
}
答案 7 :(得分:1)
是的,你可以使用很多库来制作像cosmos Cosmos library for rating control
这样的控件如果你想要这样的东西看起来像评级图(如图中的那个)那么你可以看到 这个回购在github Rating graph view in iOS
答案 8 :(得分:0)
如果你想要的只是整数星星,你可以使用一个简单的字符串数组并用它来设置UILabel的文本属性。
let ratingsDisplay = [
"★☆☆☆☆",
"★★☆☆☆",
"★★★☆☆",
"★★★★☆",
"★★★★★"
]
let rating = 4
let label = UILabel()
label.text = ratingsDisplay[rating - 1]
确保将UILabel的字体设置为以相同尺寸显示黑色和白色星星的字体。很多但不是全部。
答案 9 :(得分:0)
使用https://github.com/shantaramk/RatingView。
轻松实现此目标。只需执行以下步骤:
设置Star View 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)
}
}