Skip to content

Commit

Permalink
added character shrink as initializable parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
tomknig committed Jun 12, 2014
1 parent c8b5a90 commit d2ee872
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
28 changes: 15 additions & 13 deletions Classes/TOMSMorphingLabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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

Expand Down Expand Up @@ -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))
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})
})
Expand Down

0 comments on commit d2ee872

Please sign in to comment.