How to use MarqueeLabel in Swift?

后端 未结 7 1626
无人及你
无人及你 2021-01-05 19:17

I want to know if there\'s a way to enable horizontal scrolling of text i.e., marquee type text. I have used this library: https://github.com/cbpowell/MarqueeLabel-Swift and

7条回答
  •  青春惊慌失措
    2021-01-05 19:51

    Updated for Swift 3 without using any third party library or pods

    import UIKit
    
    class ViewController: UIViewController {
    
        let redLabel: UILabel = {
    
            let label = UILabel()
            label.translatesAutoresizingMaskIntoConstraints = false
            label.font = .boldSystemFont(ofSize: 16)
            label.textColor = .red
            label.text = "The first paragraph of the body should contain the strongest argument, most significant example, cleverest illustration, or an obvious beginning point."
            return label
        }()
    
        let greenLabel: UILabel = {
    
            let label = UILabel()
            label.translatesAutoresizingMaskIntoConstraints = false
            label.font = .systemFont(ofSize: 14)
            label.textColor = .green
            label.text = "The second paragraph of the body should contain the second strongest argument, second most significant example, second cleverest illustration, or an obvious follow up the first paragraph in the body."
            return label
        }()
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            title = "Marquee Label Demo"
            view.backgroundColor = .white
    
            view.addSubview(redLabel)
            view.addSubview(greenLabel)
    
            setupAutoLayout()
            startMarqueeLabelAnimation()
        }
    
        func startMarqueeLabelAnimation() {
    
            DispatchQueue.main.async(execute: {
    
                UIView.animate(withDuration: 10.0, delay: 1, options: ([.curveLinear, .repeat]), animations: {() -> Void in
    
                    self.redLabel.center = CGPoint(x: 0 - self.redLabel.bounds.size.width / 2, y: self.redLabel.center.y)
                    self.greenLabel.center = CGPoint(x: 0 - self.greenLabel.bounds.size.width / 2, y: self.greenLabel.center.y)
    
                }, completion:  nil)
            })
        }
    
        func setupAutoLayout() {
    
            redLabel.leftAnchor.constraint(equalTo: view.rightAnchor).isActive = true
            redLabel.topAnchor.constraint(equalTo: view.topAnchor, constant: 100).isActive = true
            redLabel.heightAnchor.constraint(equalToConstant: 20).isActive = true
    
            greenLabel.leftAnchor.constraint(equalTo: view.rightAnchor).isActive = true
            greenLabel.topAnchor.constraint(equalTo: redLabel.bottomAnchor, constant: 50).isActive = true
            greenLabel.heightAnchor.constraint(equalToConstant: 20).isActive = true
        }
    }
    

    Special Thanks:

    https://stackoverflow.com/users/8303852/kartik-patel

    https://stackoverflow.com/users/8388863/sid-patel

    click here to see demo 1

提交回复
热议问题