0

In my app, I show the title of an audio track in a UILabel. Based on the length of the name string, it can overflow the label (...) and not be visible.

To improve this, I'd like to have the label scroll horizontally (this is called a marquee animation elsewhere); however, I want it to only animate when the text overflows the view (so if a title is short, it would not scroll horizontally) and show the whole name.

In my view, I run:

override func viewDidLoad() {

   episodeTitle.text = testName

    // scroll episode name test

    UIView.animate(withDuration: 12.0, delay: 1, options: ([.curveLinear, .repeat]), animations: {() -> Void in
        self.episodeTitle.center = CGPoint(x: 0 - self.episodeTitle.bounds.size.width / 2, y: self.episodeTitle.center.y)
    }, completion:  { _ in })

}

@IBOutlet var episodeTitle: UILabel!

When I run this, it either cuts off the portion outside the view (if Line Break is set to truncate or scrolls both lines if I change Lines to 2 and set it to wrap).

Is there a way to handle this programmatically to animate the entire text string without cutting it off?

Note: I did see other questions related to marquee behavior but have not found a solution that does not require a 3P Pod to handle overflow text, hence the new question.

darkginger
  • 652
  • 1
  • 10
  • 38
  • Here is a nice implementation of this [MarqueeLabel](https://github.com/cbpowell/MarqueeLabel) – Rocky Feb 27 '18 at 05:57

1 Answers1

0

Can't you, instead of this, animate removing the head of the displayed text and appending on the last piece of text. i.e . instead of animating the label moving use a timer to add one character (and remove one) on each tick of the timer? You will then be guaranteed not to cut off any of the text.

stevenpcurtis
  • 1,907
  • 3
  • 21
  • 47