diff --git a/Classes/TOMSMorphingLabel.swift b/Classes/TOMSMorphingLabel.swift index 9219b96..a2558fe 100644 --- a/Classes/TOMSMorphingLabel.swift +++ b/Classes/TOMSMorphingLabel.swift @@ -17,30 +17,32 @@ class TOMSMorphingLabel: UILabel { let numberOfAttributionStages: Int! let animationDuration: Double! - let charAnimationOffset: Double! + let characterAnimationOffset: Double! + let characterShrinkFactor: Double! @lazy var attributionStages: NSMutableDictionary[] = self.attributionStagesForNumberOfStages(self.numberOfAttributionStages) convenience init(frame: CGRect) { - self.init(frame: frame, animationDuration: 0.21, charAnimationOffset: 0.25, fps: 60) + self.init(frame: frame, animationDuration: 0.36) } - init(frame: CGRect, animationDuration: Double, charAnimationOffset: Double, fps: Int) { + init(frame: CGRect, animationDuration: Double, characterAnimationOffset: Double = 0.25, characterShrinkFactor: Double = 4, fps: Int = 60) { super.init(frame: frame) self.animationDuration = animationDuration - self.charAnimationOffset = charAnimationOffset + self.characterAnimationOffset = characterAnimationOffset + self.characterShrinkFactor = characterShrinkFactor self.numberOfAttributionStages = Int(Double(fps) * animationDuration) } func attributionStagesForNumberOfStages(numberOfStages: Int) -> NSMutableDictionary[] { var attributionStages = NSMutableDictionary[]() - let minFontSize: Float = Float(self.font.pointSize) / (1.61 * 2) - let fontRatio: Float = minFontSize / Float(self.font.pointSize) - let fontPadding: Float = 1 - fontRatio + let minFontSize: Double = Double(self.font.pointSize) / self.characterShrinkFactor + let fontRatio: Double = minFontSize / Double(self.font.pointSize) + let fontPadding: Double = 1 - fontRatio - func ease(x: Float) -> Float { + func ease(x: Double) -> Double { if (x < 0.5) { return 2 * x * x; } @@ -62,12 +64,12 @@ class TOMSMorphingLabel: UILabel { for i in 0..numberOfStages { var attributionStage = NSMutableDictionary() - let progress = ease(Float(i) / Float(numberOfStages - 1)) + let progress = ease(Double(i) / Double(numberOfStages - 1)) let textColor = textColorWithAlpha(CGFloat(progress)) attributionStage[NSForegroundColorAttributeName] = textColor - let fontScale = fontRatio + progress * fontPadding - attributionStage[NSFontAttributeName] = fontOfScale(CGFloat(fontScale)) + let fontScale = CGFloat(fontRatio + progress * fontPadding) + attributionStage[NSFontAttributeName] = fontOfScale(fontScale) attributionStage[kernPercentageAttributeName] = 1 - progress @@ -136,7 +138,7 @@ class TOMSMorphingLabel: UILabel { let middle = countElements(string) / 2 for range in mutations { - let entryPoint = AnimationDirection.entryPoint(range.location, middle: middle) * self.charAnimationOffset * Double(self.numberOfAttributionStages) + let entryPoint = AnimationDirection.entryPoint(range.location, middle: middle) * self.characterAnimationOffset * Double(self.numberOfAttributionStages) var idx = Int(offset - entryPoint) idx = min(self.numberOfAttributionStages - 1, max(0, idx)) @@ -151,7 +153,7 @@ class TOMSMorphingLabel: UILabel { } } - applyMutations(self.rangesOfAdditions, Double(stage) * (1 + self.charAnimationOffset)) + applyMutations(self.rangesOfAdditions, Double(stage) * (1 + self.characterAnimationOffset)) applyMutations(self.rangesOfDeletions, Double(self.numberOfAttributionStages - stage)) self.attributedText = mutableString diff --git a/TOMSMorphingLabelExample/TOMSMorphingLabelExample/ViewController.swift b/TOMSMorphingLabelExample/TOMSMorphingLabelExample/ViewController.swift index 2568d53..267bb35 100644 --- a/TOMSMorphingLabelExample/TOMSMorphingLabelExample/ViewController.swift +++ b/TOMSMorphingLabelExample/TOMSMorphingLabelExample/ViewController.swift @@ -24,22 +24,26 @@ class ViewController: UIViewController { let textLabelFrame = CGRect(x: 0, y: 42, width: self.view.frame.size.width, height: 42) var textLabel = TOMSMorphingLabel(frame: textLabelFrame) - textLabel.font = UIFont.systemFontOfSize(24) + textLabel.font = UIFont.systemFontOfSize(32) textLabel.textColor = UIColor(red: 0.102, green: 0.839, blue: 0.992, alpha: 1) textLabel.textAlignment = NSTextAlignment.Center self.view.addSubview(textLabel) + self.insertTestText(textLabel) } func insertTestText(label: UILabel) { + label.text = "Swift" dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.0 * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), { - label.text = "Swift" + label.text = "Swiftilicious" dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.0 * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), { - label.text = "Swiftilicious" + label.text = "delicious" dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.0 * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), { - label.text = "delicious" - self.insertTestText(label) + label.text = "" + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.0 * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), { + self.insertTestText(label) + }) }) }) })