ProgressBar molecule with a Label

This commit is contained in:
Panth Patel 2019-05-09 17:31:18 -04:00
parent 350385a499
commit 2bd55296b8
2 changed files with 33 additions and 61 deletions

View File

@ -10,53 +10,36 @@ import Foundation
public class ProgressBar: UIProgressView { public class ProgressBar: UIProgressView {
public func styleprogessbar(json: Dictionary<String,Any>) { public func styleprogessbar(json: [AnyHashable: Any]?) {
let percentage = Float(json["percent"] as! String)
let barstyle = json["barStyle"] as! Bool var barstyle = Bool()
let progresscolor = json["progressColor"] as! String var progresscolor = String()
let backgroundcolor = json["backgroundColor"] as! String var backgroundcolor = String()
let thickness = Float(json["thickness"] as! String)
let thickness = json?.floatFromStringForKey("thickness")
let percentage = json?.floatFromStringForKey("percent")
if let backgroundcolorUnwrapped = json?.optionalStringForKey("backgroundColor") {
backgroundcolor = backgroundcolorUnwrapped
}
if let progresscolorUnwrapped = json?.optionalStringForKey("progressColor") {
progresscolor = progresscolorUnwrapped
}
if let barStyleUnwrapped = json?["barStyle"] as? Bool {
barstyle = barStyleUnwrapped
}
progressTintColor = UIColor.mfGet(forHex: progresscolor)
trackTintColor = UIColor.mfGet(forHex: backgroundcolor)
progress = (percentage ?? Float(PaddingThree))/100
self.progressTintColor = UIColor.init(hex: progresscolor)
self.trackTintColor = UIColor.init(hex: backgroundcolor)
self.progress = percentage!/100
self.frame.size.width = 200
switch barstyle { switch barstyle {
case true: case true:
self.progressViewStyle = .bar progressViewStyle = .bar
default: default:
self.layer.cornerRadius = CGFloat(thickness!/2) layer.cornerRadius = CGFloat((thickness ?? Float(PaddingTwo))/2)
self.clipsToBounds = true clipsToBounds = true
} }
} }
} }
extension UIColor {
public convenience init?(hex: String) {
let r, g, b, a: CGFloat
if hex.hasPrefix("#") {
let start = hex.index(hex.startIndex, offsetBy: 1)
let hexColor = String(hex[start...])
if hexColor.count == 6 {
let scanner = Scanner(string: hexColor)
var hexNumber: UInt32 = 0
if scanner.scanHexInt32(&hexNumber) {
r = CGFloat((hexNumber & 0xff0000) >> 16) / 255
g = CGFloat((hexNumber & 0x00ff00) >> 8) / 255
b = CGFloat(hexNumber & 0x0000ff) / 255
self.init(red: r, green: g, blue: b, alpha: 1.0)
return
}
}
}
return nil
}
}

View File

@ -9,7 +9,7 @@
import UIKit import UIKit
@objcMembers open class ProgressBarView: ViewConstrainingView { @objcMembers open class ProgressBarView: ViewConstrainingView {
var progress = ProgressBar() var progress = ProgressBar()
var descriptionLabel = Label() var descriptionLabel = Label()
var thickness: Float? var thickness: Float?
@ -37,42 +37,31 @@ import UIKit
open override func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: DelegateObject?, additionalData: [AnyHashable: Any]?) { open override func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: DelegateObject?, additionalData: [AnyHashable: Any]?) {
super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData) super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData)
progress.styleprogessbar(json: json)
let thickness = json?.floatFromStringForKey("thickness")
let textlabeljson = json?.optionalDictionaryForKey("label")
if let jsonUnwrapped = json as? Dictionary<String, Any> { descriptionLabel.setWithJSON(textlabeljson, delegateObject: delegateObject, additionalData: additionalData)
progress.styleprogessbar(json: jsonUnwrapped) progress.heightAnchor.constraint(equalToConstant: CGFloat(thickness ?? Float(PaddingTwo))).isActive = true
}
let textlabeljson = json!["label"] as! Dictionary<String,Any> //UILabel.init(frame: CGRect(x:0, y:0, width: 300, height: 300))
self.descriptionLabel.setWithJSON(textlabeljson, delegateObject: delegateObject, additionalData: additionalData)
thickness = Float(json!["thickness"] as! String)
// thickness = 20.0
} }
override open func setupView() { override open func setupView() {
super.setupView() super.setupView()
translatesAutoresizingMaskIntoConstraints = false
addSubview(descriptionLabel) addSubview(descriptionLabel)
addSubview(progress) addSubview(progress)
let vericalSpacing = MFStyler.defaultVerticalPaddingForApplicationWidth()
progress.translatesAutoresizingMaskIntoConstraints = false progress.translatesAutoresizingMaskIntoConstraints = false
descriptionLabel.translatesAutoresizingMaskIntoConstraints = false descriptionLabel.translatesAutoresizingMaskIntoConstraints = false
let vericalSpacing = MFStyler.defaultVerticalPaddingForApplicationWidth()
descriptionLabel.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true descriptionLabel.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
descriptionLabel.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true descriptionLabel.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
descriptionLabel.topAnchor.constraint(equalTo: self.topAnchor).isActive = true descriptionLabel.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
descriptionLabel.numberOfLines = 0
descriptionLabel.sizeToFit()
progress.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true progress.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
progress.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true progress.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
progress.heightAnchor.constraint(equalToConstant: 20).isActive = true
progress.topAnchor.constraint(equalTo: descriptionLabel.bottomAnchor, constant: vericalSpacing).isActive = true progress.topAnchor.constraint(equalTo: descriptionLabel.bottomAnchor, constant: vericalSpacing).isActive = true
progress.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true progress.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
} }
} }