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.