MarqueeLabel icon indicating copy to clipboard operation
MarqueeLabel copied to clipboard

How to wrap MarqueeLabel with using SwiftUI?

Open sudachi808 opened this issue 2 years ago • 1 comments

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()
    }
}

sudachi808 avatar Jun 03 '22 09:06 sudachi808

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()
    }

davidkessler-ch avatar Jul 04 '22 14:07 davidkessler-ch