diff --git a/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata b/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/LMGaugeViewSwift/Classes/GaugeView.swift b/LMGaugeViewSwift/Classes/GaugeView.swift index 0f5bb45..2fa071a 100644 --- a/LMGaugeViewSwift/Classes/GaugeView.swift +++ b/LMGaugeViewSwift/Classes/GaugeView.swift @@ -8,10 +8,10 @@ import Foundation import UIKit -public protocol GaugeViewDelegate: class { +public protocol GaugeViewDelegate: AnyObject { /// Return ring stroke color from the specified value. - func ringStokeColor(gaugeView: GaugeView, value: Double) -> UIColor + func ringStokeColor(gaugeView: GaugeView, value: Double?) -> UIColor } open class GaugeView: UIView { @@ -22,24 +22,31 @@ open class GaugeView: UIView { public static let defaultMinMaxValueFont = "HelveticaNeue" + public static let unitOfMeasurementLabelHeight = 20 + /// Current value. - public var value: Double = 0 { + public var value: Double? = nil { didSet { - value = max(min(value, maxValue), minValue) - // Set text for value label - valueLabel.text = String(format: "%.0f", value) + showValueLabelText() // Trigger the stoke animation of ring layer strokeGauge() } } + /// Decimal places + public var decimalPlaces: Int = 1 { + didSet { + decimalPlacesFormatString = "%.\(decimalPlaces)f" + } + } + /// Minimum value. public var minValue: Double = 0 /// Maximum value. - public var maxValue: Double = 120 + public var maxValue: Double = 100 /// Limit value. public var limitValue: Double = 50 @@ -114,6 +121,7 @@ open class GaugeView: UIView { var endAngle: Double = .pi/4 + .pi * 2 var divisionUnitAngle: Double = 0 var divisionUnitValue: Double = 0 + var decimalPlacesFormatString = "%.1f" lazy var progressLayer: CAShapeLayer = { let layer = CAShapeLayer() @@ -125,7 +133,7 @@ open class GaugeView: UIView { return layer }() - lazy var valueLabel: UILabel = { + lazy public var valueLabel: UILabel = { let label = UILabel() label.backgroundColor = UIColor.clear label.textAlignment = .center @@ -250,21 +258,22 @@ open class GaugeView: UIView { if valueLabel.superview == nil { addSubview(valueLabel) } - valueLabel.text = String(format: "%.0f", value) + showValueLabelText() valueLabel.font = valueFont - valueLabel.minimumScaleFactor = 10/valueFont.pointSize + valueLabel.minimumScaleFactor = 0.5 valueLabel.textColor = valueTextColor let insetX = ringThickness + divisionsPadding * 2 + divisionsRadius - valueLabel.frame = progressLayer.frame.insetBy(dx: CGFloat(insetX), dy: CGFloat(insetX)) + let insetY = showUnitOfMeasurement ? insetX + Double(GaugeView.unitOfMeasurementLabelHeight) : insetX + valueLabel.frame = progressLayer.frame.insetBy(dx: CGFloat(insetX), dy: CGFloat(insetY)) valueLabel.frame = valueLabel.frame.offsetBy(dx: 0, dy: showUnitOfMeasurement ? CGFloat(-divisionsPadding/2) : 0) // Min Value Label if minValueLabel.superview == nil { addSubview(minValueLabel) } - minValueLabel.text = String(format: "%.0f", minValue) + minValueLabel.text = String(format: decimalPlacesFormatString, minValue) minValueLabel.font = minMaxValueFont - minValueLabel.minimumScaleFactor = 10/minMaxValueFont.pointSize + minValueLabel.minimumScaleFactor = 0.5 minValueLabel.textColor = minMaxValueTextColor minValueLabel.isHidden = !showMinMaxValue let minDotCenter = CGPoint(x: CGFloat(dotRadius * cos(startAngle)) + center.x, @@ -275,9 +284,9 @@ open class GaugeView: UIView { if maxValueLabel.superview == nil { addSubview(maxValueLabel) } - maxValueLabel.text = String(format: "%.0f", maxValue) + maxValueLabel.text = String(format: decimalPlacesFormatString, maxValue) maxValueLabel.font = minMaxValueFont - maxValueLabel.minimumScaleFactor = 10/minMaxValueFont.pointSize + maxValueLabel.minimumScaleFactor = 0.5 maxValueLabel.textColor = minMaxValueTextColor maxValueLabel.isHidden = !showMinMaxValue let maxDotCenter = CGPoint(x: CGFloat(dotRadius * cos(endAngle)) + center.x, @@ -290,19 +299,24 @@ open class GaugeView: UIView { } unitOfMeasurementLabel.text = unitOfMeasurement unitOfMeasurementLabel.font = unitOfMeasurementFont - unitOfMeasurementLabel.minimumScaleFactor = 10/unitOfMeasurementFont.pointSize + unitOfMeasurementLabel.minimumScaleFactor = 0.5 unitOfMeasurementLabel.textColor = unitOfMeasurementTextColor unitOfMeasurementLabel.isHidden = !showUnitOfMeasurement unitOfMeasurementLabel.frame = CGRect(x: valueLabel.frame.origin.x, y: valueLabel.frame.maxY - 10, width: valueLabel.frame.width, - height: 20) + height: CGFloat(GaugeView.unitOfMeasurementLabelHeight)) } public func strokeGauge() { // Set progress for ring layer - let progress = maxValue != 0 ? (value - minValue)/(maxValue - minValue) : 0 + let progress: Double + if let value = value { + progress = maxValue != 0 ? (value - minValue)/(maxValue - minValue) : 0 + } else { + progress = 0 + } progressLayer.strokeEnd = CGFloat(progress) // Set ring stroke color @@ -312,6 +326,19 @@ open class GaugeView: UIView { } progressLayer.strokeColor = ringColor.cgColor } + + + private func showValueLabelText() { + if let value = value { + if value.isNaN { + valueLabel.text = "-" + } else { + valueLabel.text = String(format: decimalPlacesFormatString, value) + } + } else { + valueLabel.text = "-" + } + } } // MARK: - SUPPORT diff --git a/Package.swift b/Package.swift new file mode 100644 index 0000000..4fc3559 --- /dev/null +++ b/Package.swift @@ -0,0 +1,30 @@ +// swift-tools-version: 5.8 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "LMGaugeViewSwift", + platforms: [ + .iOS(.v11) + ], + products: [ + // Products define the executables and libraries a package produces, and make them visible to other packages. + .library( + name: "LMGaugeViewSwift", + targets: ["LMGaugeViewSwift"]), + ], + dependencies: [ + // Dependencies declare other packages that this package depends on. + // .package(url: /* package url */, from: "1.0.0"), + ], + targets: [ + // Targets are the basic building blocks of a package. A target can define a module or a test suite. + // Targets can depend on other targets in this package, and on products in packages this package depends on. + .target( + name: "LMGaugeViewSwift", + dependencies: [], + path: "LMGaugeViewSwift/Classes" + ) + ] +) diff --git a/README.md b/README.md index 6a0cc36..768a046 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # LMGaugeViewSwift [![CI Status](https://img.shields.io/travis/LMinh/LMGaugeViewSwift.svg?style=flat)](https://travis-ci.org/LMinh/LMGaugeViewSwift) +![Swift-Package](https://img.shields.io/badge/Swift--Package-1.1.0-brightgreen) [![Version](https://img.shields.io/cocoapods/v/LMGaugeViewSwift.svg?style=flat)](https://cocoapods.org/pods/LMGaugeViewSwift) [![License](https://img.shields.io/cocoapods/l/LMGaugeViewSwift.svg?style=flat)](https://cocoapods.org/pods/LMGaugeViewSwift) [![Platform](https://img.shields.io/cocoapods/p/LMGaugeViewSwift.svg?style=flat)](https://cocoapods.org/pods/LMGaugeViewSwift) @@ -19,7 +20,7 @@ https://github.com/lminhtm/LMGaugeView ## Requirements * iOS 8.0 or higher -## Installation +## CocoaPods Installation LMGaugeViewSwift is available through [CocoaPods](https://cocoapods.org). To install it, simply add the following line to your Podfile: @@ -27,6 +28,17 @@ it, simply add the following line to your Podfile: pod 'LMGaugeViewSwift' ``` +## Swift-Package Installation +Open your project in Xcode + +1. Click "File" -> "Add Packages..." +2. Paste the following URL: https://github.com/gallinaettore/LMGaugeViewSwift + +You can specify the dependency in Package.swift by adding this: +```ruby +.package(url: "https://github.com/gallinaettore/LMGaugeViewSwift.git", .upToNextMajor(from: "1.1.0")) +``` + ## Example To run the example project, clone the repo, and run `pod install` from the Example directory first.