Gibt es Steuerelemente für die Sternebewertung in iOS? Also kann ich das in UITableviewCell verwenden?
Ich weiß, dass es einige Open-Source-Steuerelemente wie DYRating usw. gibt. Aber ich bin nicht in der Lage, es in der Tabellenzelle hinzuzufügen.
Sie können die ASStar-Bewertung überprüfen.
Fügen Sie das Steuerelement direkt Ihrem Ansichtscontroller hinzu oder fügen Sie es in Ihre Zelle ein.
Und hier ist github link .
Ich empfehle HCSStarRatingView . Es sieht nativ aus und unterstützt auch IB_DESIGNABLE
und IBInspectable
.
Schauen Sie mal rein, es sollte Ihnen gut passen:
https://www.cocoacontrols.com/search?utf8=%E2%9C%93&q=star+rating
Hinweis: Dies ist ein "Nur Anzeige".
Ich habe hier einen anderen Ansatz gewählt, ich habe ein Label mit zugeordneter Zeichenfolge erstellt, wobei eine Schrift mit Sternen verwendet wird. Sie können meine Kategorie hier überprüfen. Ich habe "SS Pika" verwendet,
#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
VERWENDUNGSZWECK:
self.ratingLabel.attributedText = [NSMutableAttributedString starWithRating:rating outOfTotal:5 withFontSize:9.0f];
Hier ist die Swift Version
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
}
}
VERWENDUNGSZWECK:
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)
Sie können die folgenden Steuerelemente in Cocoacontrols überprüfen.
https://www.cocoacontrols.com/controls/star-rating-view
Swift3
https://github.com/marketplacer/Cosmos
install pod
target 'TargetName' do
pod 'Cosmos'
Installiere die Pod-Datei mit diesem Befehl pod install
Füge eine einfache UIView in dein Storyboard ein und gib den Klassennamen als CosmosView
und Modulnamen Cosmos
ein.
HIER KÖNNEN SIE AUF DIE ATTRIBUTE EINZIEHEN
Verwenden Sie https://github.com/hugocampossousa/HCSStarRatingView . es ist leicht, dies umzusetzen. Schreiben Sie einfach diese Codezeilen auf. Erstellen Sie IBOutlet HCSStarRatingView für die .h-Datei und erstellen Sie eine Zeichenfolge zum Speichern des Bewertungswerts.
@property (weak, nonatomic) IBOutlet HCSStarRatingView *starRatingView;
@property (weak, nonatomic) NSString *ratingStr;
schreiben Sie diese Zeilen in eine .m-Datei
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;
}
}
Ja, es gibt viele Bibliotheken, die Sie verwenden können, um solche Steuerelemente wie Cosmos zu erstellen. Cosmos-Bibliothek zur Bewertungssteuerung
wenn Sie möchten, dass etwas wie Bewertungsgraph (wie das im Bild) aussieht, können Sie dieses Repo in github Bewertungsgraphansicht in iOS sehen. _
Ich habe gerade diese einfache UIView-Unterklasse geschrieben, die Sterne von 0 bis 5 einschließlich halber Sterne zeichnet. Sie können die Farbe mit der tintColor-Eigenschaft festlegen und die Bewertung im Oberflächeneditor festlegen. Die Ansichtsbreite sollte auf 5 * Höhe eingestellt werden.
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)
}
}
}
}
Wenn Sie nur eine ganze Anzahl von Sternen wünschen, können Sie ein einfaches String-Array verwenden, um die Texteigenschaft eines UILabels festzulegen.
let ratingsDisplay = [
"★☆☆☆☆",
"★★☆☆☆",
"★★★☆☆",
"★★★★☆",
"★★★★★"
]
let rating = 4
let label = UILabel()
label.text = ratingsDisplay[rating - 1]
Stellen Sie sicher, dass Sie die Schriftart des UILabels so einstellen, dass die schwarzen und weißen Sterne in derselben Größe angezeigt werden. Viele, aber nicht alle.
Verwenden Sie https://github.com/shantaramk/RatingView .
Dies lässt sich mühelos umsetzen. Folgen Sie einfach den folgenden Schritten:
Star View IBOutlet setzen
@IBOutlet weak var rateView: StarRateView!
Star Rate View vorbereiten
func setupStarRateView() {
self.rateView.delegate = self
self.rateView.maxCount = 5
self.rateView.fillImage = UIImage(named: "fill_star")!
self.rateView.emptyImage = UIImage(named: "empty_star")!
}
Rate View Delegate übernehmen
extension RateServiceViewController: RatingViewDelegate {
func updateRatingFormatValue(_ value: Int) {
print("Rating : = ", value)
}
}