From 8e7538aaa49b05ecb35a99f951f862d936361d3d Mon Sep 17 00:00:00 2001 From: EyreFree Date: Wed, 30 Jan 2019 17:23:30 +0800 Subject: [PATCH] Fix example --- .../Classes/EFCountingButton.swift | 1 + Example/EFCountingLabel/ViewController.swift | 21 ++++++++++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/EFCountingLabel/Classes/EFCountingButton.swift b/EFCountingLabel/Classes/EFCountingButton.swift index ef89655..b5aa88d 100644 --- a/EFCountingLabel/Classes/EFCountingButton.swift +++ b/EFCountingLabel/Classes/EFCountingButton.swift @@ -51,6 +51,7 @@ public class EFCountingButtonLabel: EFCountingLabel { } } +// EFCountingButton.titleLabel.font = xxx do not work, use NSAttributedString instead as in example project. open class EFCountingButton: UIButton { open lazy var countingLabel: EFCountingButtonLabel = { diff --git a/Example/EFCountingLabel/ViewController.swift b/Example/EFCountingLabel/ViewController.swift index a875152..1d8624d 100644 --- a/Example/EFCountingLabel/ViewController.swift +++ b/Example/EFCountingLabel/ViewController.swift @@ -99,12 +99,19 @@ class ViewController: UIViewController { self?.label.textColor = UIColor(red: 0, green: 0.5, blue: 0, alpha: 1) } - // button + // button: a simple countdown button self.countingButton = EFCountingButton(frame: CGRect(x: 10, y: 170, width: 200, height: 40)) - self.countingButton.setTitle("倒计时按钮", for: UIControl.State.normal) + self.countingButton.setTitle("CountDown", for: .normal) + let attrs = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12), NSAttributedString.Key.foregroundColor: UIColor.blue] + let attributedString = NSAttributedString(string: countingButton.title(for: UIControl.State.normal) ?? "", attributes: attrs) + self.countingButton.setAttributedTitle(attributedString, for: UIControl.State.normal) + self.countingButton.contentHorizontalAlignment = .left + self.countingButton.countingLabel.attributedFormatBlock = { [weak self] (value) in + guard let _ = self else { return NSAttributedString() } + return NSAttributedString(string: String(format: "%.0lf", value), attributes: attrs) + } self.countingButton.setTitleColor(UIColor.blue, for: UIControl.State.normal) self.countingButton.addTarget(self, action: #selector(buttonClicked), for: UIControl.Event.touchUpInside) - self.countingButton.countingLabel.format = "%d" self.countingButton.countingLabel.method = .linear self.view.addSubview(countingButton) } @@ -134,10 +141,14 @@ class ViewController: UIViewController { } @objc func buttonClicked(button: EFCountingButton) { + let titleBackup: NSAttributedString? = button.attributedTitle(for: UIControl.State.normal) self.countingButton.countingLabel.completionBlock = { [weak self] in + guard let _ = self else { return } + button.setAttributedTitle(titleBackup, for: UIControl.State.normal) + } + DispatchQueue.main.async { [weak self] in guard let strongSelf = self else { return } - strongSelf.countingButton.setTitle("倒计时按钮", for: UIControl.State.normal) + strongSelf.countingButton.countingLabel.countFrom(10, to: 0, withDuration: 10) } - self.countingButton.countingLabel.countFrom(60, to: 0, withDuration: 60) } }