Skip to content

Commit

Permalink
Merge pull request #7 from trafi/feature/grid-styling
Browse files Browse the repository at this point in the history
Added styling
  • Loading branch information
Ignelios authored Sep 9, 2020
2 parents 14832d1 + 0ddc1cb commit d04c887
Show file tree
Hide file tree
Showing 6 changed files with 268 additions and 125 deletions.
29 changes: 22 additions & 7 deletions Example.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,32 @@ let thumb = Thumb(size: 34,
color: .white,
shadowColor: .black)

let frame = CGRect(x: 20, y: 100, width: 260, height: 50)

let ratingSlider = RatingSlider(frame: frame,
gridStyle: .dots(size: 3, gridHeight: 16, hasUpperGrid: true),
thumb: thumb)
let frame = CGRect(x: 20, y: 100, width: 260, height: 60)

let dottedStyle = GridStyle.dotted(.init(
activeColor: .blue,
activeSize: 6,
inactiveColor: .brown,
inactiveSize: 3,
labels: .init(
activeColor: .red,
inactiveColor: .green)
)
)

let labeledStyle = GridStyle.labeled(.init(
activeFont: .boldSystemFont(ofSize: 12),
activeColor: .red,
inactiveFont: .italicSystemFont(ofSize: 6),
inactiveColor: .blue)
)

let ratingSlider = RatingSlider(frame: frame, gridStyle: dottedStyle, thumb: thumb)

ratingSlider.activeTrackColor = #colorLiteral(red: 0.9425747991, green: 0.8432862163, blue: 0.1268348098, alpha: 1)
ratingSlider.inactiveTrackColor = #colorLiteral(red: 0.8508961797, green: 0.8510394692, blue: 0.850877285, alpha: 1)
ratingSlider.activeColor = #colorLiteral(red: 0.6548359394, green: 0.6549482346, blue: 0.6548211575, alpha: 1)
ratingSlider.inactiveColor = #colorLiteral(red: 0.6548359394, green: 0.6549482346, blue: 0.6548211575, alpha: 1)
ratingSlider.range = 0...10
ratingSlider.startThumbSwingAnimation(power: 2.5, duration: 0.3)

container.addSubview(ratingSlider)

Expand Down
4 changes: 4 additions & 0 deletions RatingSlider.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
C02A2B202382CE61004FDA6C /* Thumb.swift in Sources */ = {isa = PBXBuildFile; fileRef = C02A2B1F2382CE61004FDA6C /* Thumb.swift */; };
C08543CE2506495100163F24 /* Appearances.swift in Sources */ = {isa = PBXBuildFile; fileRef = C08543CD2506495100163F24 /* Appearances.swift */; };
D33050831DCCBDC000798056 /* RatingSliderGrid.swift in Sources */ = {isa = PBXBuildFile; fileRef = D33050821DCCBDC000798056 /* RatingSliderGrid.swift */; };
D359D7B51DCB761100B2532E /* RatingSlider.h in Headers */ = {isa = PBXBuildFile; fileRef = D359D7B31DCB761100B2532E /* RatingSlider.h */; settings = {ATTRIBUTES = (Public, ); }; };
D359D7BD1DCB767900B2532E /* RatingSlider.swift in Sources */ = {isa = PBXBuildFile; fileRef = D359D7BC1DCB767900B2532E /* RatingSlider.swift */; };
Expand All @@ -16,6 +17,7 @@

/* Begin PBXFileReference section */
C02A2B1F2382CE61004FDA6C /* Thumb.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Thumb.swift; sourceTree = "<group>"; };
C08543CD2506495100163F24 /* Appearances.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Appearances.swift; sourceTree = "<group>"; };
D33050821DCCBDC000798056 /* RatingSliderGrid.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RatingSliderGrid.swift; sourceTree = "<group>"; };
D359D7B01DCB761100B2532E /* RatingSlider.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = RatingSlider.framework; sourceTree = BUILT_PRODUCTS_DIR; };
D359D7B31DCB761100B2532E /* RatingSlider.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RatingSlider.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -69,6 +71,7 @@
D359D7BC1DCB767900B2532E /* RatingSlider.swift */,
D33050821DCCBDC000798056 /* RatingSliderGrid.swift */,
D359D7BE1DCB769E00B2532E /* CountableClosedRangeHelper.swift */,
C08543CD2506495100163F24 /* Appearances.swift */,
C02A2B1F2382CE61004FDA6C /* Thumb.swift */,
);
path = RatingSlider;
Expand Down Expand Up @@ -158,6 +161,7 @@
D359D7BF1DCB769E00B2532E /* CountableClosedRangeHelper.swift in Sources */,
D359D7BD1DCB767900B2532E /* RatingSlider.swift in Sources */,
C02A2B202382CE61004FDA6C /* Thumb.swift in Sources */,
C08543CE2506495100163F24 /* Appearances.swift in Sources */,
D33050831DCCBDC000798056 /* RatingSliderGrid.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
95 changes: 95 additions & 0 deletions Sources/RatingSlider/Appearances.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import Foundation
import UIKit

public enum GridStyle {
case labeled(LabelAppearance)
case dotted(DotAppearance)

var hasUpperLabeledGrid: Bool {
guard case .dotted(let appearance) = self, appearance.labels != nil else { return false }
return true
}

var labeledGridHeight: CGFloat? {
guard case .dotted(let appearance) = self, let labeledAppearance = appearance.labels else { return nil }
return max(labeledAppearance.inactiveFont.textHeight(), labeledAppearance.activeFont.textHeight())
}
}

// MARK: - Label

public struct LabelAppearance {

public let activeFont: UIFont
public let activeColor: UIColor
public let inactiveFont: UIFont
public let inactiveColor: UIColor

public init(
activeFont: UIFont = .systemFont(ofSize: 12, weight: .bold),
activeColor: UIColor = .black,
inactiveFont: UIFont = .systemFont(ofSize: 12, weight: .regular),
inactiveColor: UIColor = .lightGray
) {
self.activeFont = activeFont
self.activeColor = activeColor
self.inactiveFont = inactiveFont
self.inactiveColor = inactiveColor
}

public static let `default`: Self = LabelAppearance()

func fontAndColor(isActive: Bool) -> (font: UIFont, color: UIColor) {
isActive ? (activeFont, activeColor) : (inactiveFont, inactiveColor)
}
}

// MARK: - Dot

public struct DotAppearance {

public let activeColor: UIColor
public let activeSize: CGFloat
public let inactiveColor: UIColor
public let inactiveSize: CGFloat

public let labels: LabelAppearance?

public init(
activeColor: UIColor = .black,
activeSize: CGFloat = 3.0,
inactiveColor: UIColor = .lightGray,
inactiveSize: CGFloat = 3.0,
labels: LabelAppearance? = nil
) {
self.activeColor = activeColor
self.activeSize = activeSize
self.inactiveColor = inactiveColor
self.inactiveSize = inactiveSize
self.labels = labels
}

public static let `default`: Self = DotAppearance()

func sizeAndColor(isActive: Bool) -> (size: CGFloat, color: UIColor) {
isActive ? (activeSize, activeColor) : (inactiveSize, inactiveColor)
}
}

private extension UIFont {

func textHeight(width: CGFloat = UIScreen.main.bounds.width) -> CGFloat {

let fakeString = " "
let constraintRect = CGSize(width: UIScreen.main.bounds.width, height: .greatestFiniteMagnitude)

let boundingBox = fakeString.boundingRect(
with: constraintRect,
options: .usesLineFragmentOrigin,
attributes: [NSAttributedString.Key.font: self],
context: nil
)

return ceil(boundingBox.height)
}
}
Loading

0 comments on commit d04c887

Please sign in to comment.