MarqueeLabel
MarqueeLabel copied to clipboard
How to wrap MarqueeLabel with using SwiftUI?
I want to use MarqueeLabel within SwiftUI views. But I could not work it using the code below.
How do I implement it?
import SwiftUI
import MarqueeLabel
struct MarqueeText: UIViewRepresentable {
func makeUIView(context: Context) -> MarqueeLabel {
MarqueeLabel(frame: .init(x: 0, y: 0, width: 100, height: 24), duration: 1.0, fadeLength: 10.0)
}
func updateUIView(_ uiView: MarqueeLabel, context: Context) {
uiView.textAlignment = .center
uiView.text = "The quick brown fox jumps over the lazy dog."
uiView.restartLabel()
}
}
// MARK: - Preview
struct MarqueeText_Previews: PreviewProvider {
static var previews: some View {
MarqueeText()
}
}
Use setContentCompressionResistancePriority
in order for the view to actually collapse. In your example, change the updateUIView to this:
func updateUIView(_ uiView: MarqueeLabel, context: Context) {
uiView.textAlignment = .center
uiView.text = "The quick brown fox jumps over the lazy dog."
uiView.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
uiView.restartLabel()
}