Uwaga: To jest "tylko wskazanie" jeden.
Podjęłam inne podejście, stworzyłem etykietę z przypisanym ciągiem, używając czcionki z gwiazdkami, możesz sprawdzić tutaj moją kategorię.Kiedyś "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
ZASTOSOWANIE:
self.ratingLabel.attributedText = [NSMutableAttributedString starWithRating:rating outOfTotal:5 withFontSize:9.0f];
Oto SWIFT Wersja
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
}
}
ZASTOSOWANIE:
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)
Spójrz na to http://www.raywenderlich.com/1768/how-to-make-a-custom-uiview-a-5-star-rating-view –
Bhargavi: ale muszę dodać, że w uitableviewcell, aby każdy wiersz mógł podać dane wejściowe do tej gwiazdy, a następnie wyświetli gwiazdę odpowiadającą ocenie – Naveen
Czy próbowałeś z tworzeniem CustomCells? –