是否有任何控制可供星级的IOS? 所以,我可以使用,在的UITableViewCell?
我知道有像DYRating等一些开放的开源控件。但亚姆不能将它的实现代码如下小区添加。
是否有任何控制可供星级的IOS? 所以,我可以使用,在的UITableViewCell?
我知道有像DYRating等一些开放的开源控件。但亚姆不能将它的实现代码如下小区添加。
您可以检查ASStar评级。
直接将控件添加到您的视图控制器也可以添加到您的细胞。
这里是GitHub的链接 。
我建议HCSStarRatingView 。 这是本地外观,它也支持IB_DESIGNABLE
和IBInspectable
。
看看,它应该适合你就好了:
https://www.cocoacontrols.com/search?utf8=%E2%9C%93&q=star+rating
注:这是一个“只显示”之一。
我采取了不同的方法在这里,我使用了星星的字体创建了属性串一个标签,你可以在这里查看我的类别。 我用“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)
您可以检查以下从Cocoacontrols控制。
https://www.cocoacontrols.com/controls/star-rating-view
https://www.cocoacontrols.com/controls/scrratingview
https://www.cocoacontrols.com/controls/amratingcontrol
Swift3
https://github.com/marketplacer/Cosmos
安装荚
target 'TargetName' do
pod 'Cosmos'
吊舱安装文件与此命令pod install
把你的故事板一个简单的UIView,给类名为CosmosView
和模块名称Cosmos
在这里您可以访问ATTRIBUTES
使用https://github.com/hugocampossousa/HCSStarRatingView 。 它容易实现这一点。 刚刚写下这几行代码。 创建IBOutlet中HCSStarRatingView在.h文件中的存储评定值创建一个字符串。
@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;
}
}
我只写了这个简单的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)
}
}
}
}
是啊有,你可以用它来使这种控制像宇宙许多图书馆宇宙库等级控制
如果你想这样的事情看起来像等级图形(如一个图像),那么你可以看到这个回购在GitHub上的iOS的评级图表视图
如果你想要的是明星整数,你可以使用一个简单的字符串数组,并用它来设置一个UILabel的文本属性。
let ratingsDisplay = [
"★☆☆☆☆",
"★★☆☆☆",
"★★★☆☆",
"★★★★☆",
"★★★★★"
]
let rating = 4
let label = UILabel()
label.text = ratingsDisplay[rating - 1]
请确保你的UILabel的字体设置为一这在同尺寸的显示器的黑色和白色的星星。 很多,但不是所有的事。
使用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)
}
}