Nick
Nick

Reputation: 37

text not displaying correctly .text should be placed in the center of the screen for the function

I am trying to display text when a person registers . The next screen shws a rotating spinner with a text "Checking Credentials......" , however the function for it is not displaying the text coreectly in the middle of the screen

func rotatingspinner()
    {
        processingView = UIView()

        processingView.frame = self.view.bounds

        processingView.backgroundColor = UIColor(red: 0.0/255.0, green: 113.0/255.0 , blue: 138.0/255.0, alpha: 0.97)

        processLabel = UILabel()



        let device = Device()


        if device == .iPhone6sPlus {
            processLabel.frame = CGRectMake(145,410,160,30)
        }
        if device == .iPhone6s {
            processLabel.frame = CGRectMake(125,375,160,30)
        }
        if device == .iPhone6Plus {
            processLabel.frame = CGRectMake(145,410,160,30)
        }
        if device == .iPhone6 {
            processLabel.frame = CGRectMake(125,375,160,30)
        }

        if device == .iPhone5s {
            processLabel.frame = CGRectMake(100,325,160,20)
        }
        if device == .iPhone5 {
            processLabel.frame = CGRectMake(100,325,160,20)
        }
        if device == .iPhone4s {
            processLabel.frame = CGRectMake(100,275,160,20)
        }
        if device == .iPhone4 {
            processLabel.frame = CGRectMake(100,275,160,20)
        }

        processLabel.text = "Checking Credentials......"
        processLabel.font = UIFont(name: "Arial", size: 15.0)
        processLabel.textColor = UIColor.whiteColor()
        processingView.addSubview(processLabel)
        self.processingView.addSubview(activityIndicator)
        activityIndicator.center = self.processingView.center
        activityIndicator.startAnimating()
        self.view.addSubview(processingView)

    }

how do i set the screensize.window to the frame and set the frame to keep the text in the middle

Upvotes: 0

Views: 78

Answers (1)

Vishnu gondlekar
Vishnu gondlekar

Reputation: 3956

As Nick has said you should start learning Auto layout. Coming to your code, you don't really need to have all that. You need to calculate frame for your label to make sure it is in centre. You can do that in following way

let labelHeight = CGFloat(30)
let labelWidth = CGFloat(160)
let labelX = (processingView.frame.size.width - labelHeight)/2
let labelY = (processingView.frame.size.height - labelWidth)/2
processLabel.frame = CGRectMake(labelX, labelY, labelWidth, labelHeight)

Upvotes: 1

Related Questions