Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for displaying titles below individual states #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions StepIndicator/AnnularLayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class AnnularLayer: CAShapeLayer {
private let flagLayer = CALayer()
private let annularPath = UIBezierPath()
lazy private var centerTextLayer = CATextLayer()
lazy private var titleLayer = CATextLayer()


static private let originalScale = CATransform3DMakeScale(1.0, 1.0, 1.0)
static private let flagImageName = "CYStepIndicator_ic_done_white"
Expand All @@ -24,6 +26,7 @@ class AnnularLayer: CAShapeLayer {
// MARK: - Properties
var tintColor:UIColor?
var displayNumber = false
var title: String = ""
var step:Int = 0
var annularDefaultColor: UIColor?

Expand Down Expand Up @@ -85,6 +88,8 @@ class AnnularLayer: CAShapeLayer {

// MARK: - Functions
func updateStatus() {
// Persist title through all the states
self.drawTitle()
if isFinished {
self.path = nil
self.drawFullCircleAnimated()
Expand Down Expand Up @@ -165,6 +170,19 @@ class AnnularLayer: CAShapeLayer {

self.addSublayer(self.centerTextLayer)
}

private func drawTitle() {
self.titleLayer.string = "\(self.title)"
self.titleLayer.frame = CGRect(x: 0, y: 0, width: 60, height: self.bounds.height)
self.titleLayer.position = CGPoint(x: self.bounds.midX, y: self.bounds.maxY * 1.75)
self.titleLayer.contentsScale = UIScreen.main.scale
self.titleLayer.foregroundColor = self.strokeColor
self.titleLayer.alignmentMode = CATextLayerAlignmentMode.center
self.titleLayer.font = UIFont.systemFont(ofSize: 10) as CFTypeRef
self.titleLayer.fontSize = 10

self.addSublayer(self.titleLayer)
}

private func animateCenter() {
self.centerCircleLayer.transform = CATransform3DMakeScale(0.8, 0.8, 1.0)
Expand Down
12 changes: 12 additions & 0 deletions StepIndicator/StepIndicatorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ public class StepIndicatorView: UIView {


// MARK: - Custom properties
public var titles: [String]? {
didSet{
self.updateSubLayers()
}
}

@IBInspectable public var numberOfSteps: Int = 5 {
didSet {
self.createSteps()
Expand Down Expand Up @@ -188,6 +194,9 @@ public class StepIndicatorView: UIView {
annularLayer.frame = CGRect(x: x, y: y - self.circleRadius, width: diameter, height: diameter)
self.applyAnnularStyle(annularLayer: annularLayer)
annularLayer.step = i + 1
if let titles = titles {
annularLayer.title = titles[i]
}
annularLayer.updateStatus()

if (i < self.numberOfSteps - 1) {
Expand All @@ -210,6 +219,9 @@ public class StepIndicatorView: UIView {
annularLayer.frame = CGRect(x: x - self.circleRadius, y: y, width: diameter, height: diameter)
self.applyAnnularStyle(annularLayer: annularLayer)
annularLayer.step = i + 1
if let titles = titles {
annularLayer.title = titles[i]
}
annularLayer.updateStatus()

if (i < self.numberOfSteps - 1) {
Expand Down