Created
May 13, 2025 15:19
-
-
Save Chronos2500/688b2bb7fad02022c53d033d7e2517d5 to your computer and use it in GitHub Desktop.
MarqueeLabel Please be fully aware that these use Private APIs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let label = UILabel() | |
| label.text = "Swift is a modern, intuitive programming language crafted for all Apple platforms." | |
| label.setValue(true, forKey: "marqueeEnabled") | |
| label.setValue(true, forKey: "marqueeRunning") | |
| label.setValue(0, forKey: "marqueeRepeatCount") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // MarqueeLabel.swift | |
| // PrivateAPISample | |
| // | |
| // Created by Chronos2500 on 2025/05/13. | |
| // | |
| import SwiftUI | |
| fileprivate let text = """ | |
| Swift is a modern, intuitive programming language crafted for all Apple platforms. | |
| """ | |
| struct MarqueeLabelView: View { | |
| var body: some View { | |
| MarqueeLabel( | |
| text, | |
| font: .preferredFont(forTextStyle: .title1) | |
| ) | |
| } | |
| } | |
| struct MarqueeLabel: UIViewRepresentable { | |
| var text: String | |
| var repeatCount: Int | |
| var font: UIFont | |
| var textColor: UIColor | |
| init( | |
| _ text: String, | |
| repeatCount: Int = 0, | |
| font: UIFont = .preferredFont(forTextStyle: .body), | |
| textColor: UIColor = .label | |
| ) { | |
| self.text = text | |
| self.repeatCount = repeatCount | |
| self.font = font | |
| self.textColor = textColor | |
| } | |
| func makeUIView(context: Context) -> UILabel { | |
| let label = UILabel() | |
| label.text = text | |
| label.textAlignment = .natural | |
| label.font = font | |
| label.textColor = textColor | |
| label.backgroundColor = .clear | |
| label.numberOfLines = 1 | |
| label.setContentCompressionResistancePriority(.defaultLow, for: .horizontal) | |
| label.setContentHuggingPriority(.defaultLow, for: .horizontal) | |
| label.setContentHuggingPriority(.required, for: .vertical) | |
| label.setContentCompressionResistancePriority(.required, for: .vertical) | |
| label.adjustsFontForContentSizeCategory = true | |
| label.setValue(true, forKey: "marqueeEnabled") | |
| label.setValue(true, forKey: "marqueeRunning") | |
| label.setValue(repeatCount, forKey: "marqueeRepeatCount") | |
| label.setValue(40, forKey: "marqueeLoopPadding") | |
| label.setValue(true, forKey: "marqueeUpdatable") | |
| return label | |
| } | |
| func updateUIView(_ uiView: UILabel, context: Context) { | |
| } | |
| } | |
| #Preview { | |
| MarqueeLabelView() | |
| } |
Very interesting, also thank you for your insights!
You can play around with what I came up with here:
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@llsc12 I'm just looking at the internal impl. Apple has an internal
_UILabelMarqueeAnimationDelegateobject which listens to animation start and end. So you would set[self setValue:@1 forKey:@"marqueeRepeatCount"];in all your labels, listen toanimationDidStop:finished:in the delegate object of the longest running label, and notify other labels to start animation again.The animation object contains the animation length, but not sure yet how
UILabelcalculates it. It's a relatively simple calc, but params such as rate are not exposed. Once I have all this implemented, I will post a small framework with an easy public API for what you need.