0

My controller allows me to show a text from a song inside a label but I don't know why the gravity of the text is focused at the end. When I want to see the text I always see the song like it's already scrolled. I want to start the song from the top and then scroll it or zoom it. Every time I want to see a song that is longer than the page I see like it's already scrolled, but if I want to see a song that is shorter than the page it works correctly. How can I fix it?? Here is my code:

import UIKit

class DettaglioCanti: UIViewController {

    var dettaglioCanzone: VociMontagna? {
        didSet {
            configureView()
        }
    }

    var valoriPassati: VociMontagna?

    @IBOutlet weak var tv_titolo: UILabel!

    @IBOutlet weak var tv_artista: UILabel!

    @IBOutlet weak var tv_testoCanzone: UITextView!

    @IBAction func btt_note(_ sender: Any) {
        startPopUp()
    }
    @IBOutlet weak var btt_note_2: UIButton!

    var pinchGesture = UIPinchGestureRecognizer()

    override func viewDidLoad() {
        super.viewDidLoad()

        configureView()
        pinchGesture = UIPinchGestureRecognizer(target: self, action:#selector(pinchText(sender:)))
        tv_testoCanzone.addGestureRecognizer(pinchGesture)
        let range = NSMakeRange(tv_testoCanzone.text.count - 1, 0)
        tv_testoCanzone.scrollRangeToVisible(range)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @objc func pinchText(sender: UIPinchGestureRecognizer) {
        var pointSize = tv_testoCanzone.font?.pointSize

        pointSize = ((sender.velocity > 0) ? 1 : -1) * 1 + pointSize!;

        //Definisco i limiti dello zoom per il testo
        if (pointSize! < 13) {pointSize = 13}
        if (pointSize! > 42) {pointSize = 42}

        tv_testoCanzone.font = UIFont( name: "arial", size: (pointSize)!)
    }

    func configureView() {
        if let dettaglioCanzone = dettaglioCanzone {
            if let tv_titolo = tv_titolo, let tv_testoCanzone = tv_testoCanzone, let tv_artista = tv_artista {
                tv_titolo.text = dettaglioCanzone.titolo
                tv_artista.text = dettaglioCanzone.artista
                tv_testoCanzone.text = loadFile(file: dettaglioCanzone.nomeTesto)
                if (dettaglioCanzone.nomeNota == "0") {btt_note_2.isHidden = true}
            }
        }
    }

    func loadFile(file name:String) -> String {
        if let path = Bundle.main.path(forResource: name, ofType: "txt") {
            if let contents = try? String(contentsOfFile: path) {
                return contents
            } else {
                print("Error! - This file doesn't contain any text.")
            }
        } else {
            print("Error! - This file doesn't exist.")
        }
        return ""
    }

}
Twing90
  • 407
  • 2
  • 5
  • 15
  • `tv_testoCanzone.scrollRangeToVisible(range)` - so you are scrolling the text to the end, and then asking why the text is scrolled to the end? Just remove this line. – mag_zbc Jul 13 '18 at 09:24
  • Possible duplicate of [Vertically align text to top within a UILabel](https://stackoverflow.com/questions/1054558/vertically-align-text-to-top-within-a-uilabel) – Lal Krishna Jul 13 '18 at 09:49

0 Answers0