Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to wrap MarqueeLabel with using SwiftUI? #281

Open
sudachi808 opened this issue Jun 3, 2022 · 1 comment
Open

How to wrap MarqueeLabel with using SwiftUI? #281

sudachi808 opened this issue Jun 3, 2022 · 1 comment

Comments

@sudachi808
Copy link

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()
    }
}
@davidkessler-ch
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants