Search
 
SCRIPT & CODE EXAMPLE
 

SWIFT

star score rating swiftui

struct StarsView: View {
  private static let MAX_RATING: Float = 5 // Defines upper limit of the rating
  private static let COLOR = Color.orange // The color of the stars

  let rating: Float
  private let fullCount: Int
  private let emptyCount: Int
  private let halfFullCount: Int

  init(rating: Float) {
    self.rating = rating
    fullCount = Int(rating)
    emptyCount = Int(StarsView.MAX_RATING - rating)
    halfFullCount = (Float(fullCount + emptyCount) < StarsView.MAX_RATING) ? 1 : 0
  }

  var body: some View {
    HStack {
      ForEach(0..<fullCount) { _ in
         self.fullStar
       }
       ForEach(0..<halfFullCount) { _ in
         self.halfFullStar
       }
       ForEach(0..<emptyCount) { _ in
         self.emptyStar
       }
     }
  }

  private var fullStar: some View {
    Image(systemName: "star.fill").foregroundColor(StarsView.COLOR)
  }

  private var halfFullStar: some View {
    Image(systemName: "star.lefthalf.fill").foregroundColor(StarsView.COLOR)
  }

  private var emptyStar: some View {
    Image(systemName: "star").foregroundColor(StarsView.COLOR)
  }
}
Comment

PREVIOUS NEXT
Code Example
Swift :: Swift Arithmetic Operators 
Swift :: chevrondownf6a06a60-2122-49d0-86a0-03ba8c532aec 
Swift :: Declare Constants in Swift 
Swift :: change button image tint color swift 
Swift :: Declare Variables in Swift 
Swift :: Swift guard with multiple conditions 
Swift :: string swift 
Swift :: Swift enum Associated Values 
Swift :: Function Call Using try Keyword Swift 
Swift :: Swift Left Shift Operator 
Swift :: Swift break statement with for loop 
Swift :: redux trong swift 
Swift :: Swift print() with terminator 
Swift :: Compare AnyObjects en Swift 
Swift :: microsoft flight simulator uses which language 
Ruby :: validates length rails 
Ruby :: hashwithindifferentaccess ruby 
Ruby :: ruby key exists 
Ruby :: ruby array includes 
Ruby :: ruby attr_accessor multiple variables 
Ruby :: head in rails 
Ruby :: ruby string to int 
Ruby :: conditional operator in ruby 
Ruby :: ruby csv parse 
Ruby :: ruby loop each with index 
Ruby :: rails convert unix timestamp to datetime 
Ruby :: ruby check if constant exists 
Ruby :: ruby omit key 
Ruby :: rails optional reference 
Ruby :: rails localhost https 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =