elpita
elpita

Reputation: 1115

How to center UILabel in Swift?

I'm trying to center some text but I it doesn't seem to be working.

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.


        let title = UILabel()
        title.text = "Some Sentence"
        title.numberOfLines = 0
        title.frame = CGRectMake(self.view.bounds.size.width/2,50,self.view.bounds.size.width, self.view.bounds.size.height) // x , y, width , height
        title.textAlignment = .Center
        title.sizeToFit()
        title.backgroundColor = UIColor.redColor()
        self.view.addSubview(title)

    }

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

}

That is the code that I'm using but this is what I get:

enter image description here

It's not center to the screen. Can someone tell me what am I doing wrong?

Upvotes: 41

Views: 77010

Answers (5)

Charles-olivier Demers
Charles-olivier Demers

Reputation: 1058

Actually what you are doing is centering the text inside the UILabel. What you want to do is to center the label. To do it you can do:

title.frame.origin = CGPoint(x: x, y: y)

If you want to center the horizontal you can do:

title.frame.origin = CGPoint(x: self.view.frame.width / 2, y: yValue)

Also if you want to center the x and y value of your label you can do:

title.frame.origin = CGPoint(x: self.view.frame.width / 2, y: self.view.frame.height / 2)

Upvotes: 11

vilas deshmukh
vilas deshmukh

Reputation: 399

Code for swift 3/4/5

Paste below code after super.viewDidLoad()

var noDataLbl : UILabel?
noDataLbl = UILabel(frame: CGRect(x: 0, y: self.view.center.y, width: 290, height: 70))
noDataLbl?.textAlignment = .center
noDataLbl?.font = UIFont(name: "Halvetica", size: 18.0)
noDataLbl?.numberOfLines = 0
noDataLbl?.text = "Replace this with your text."
noDataLbl?.lineBreakMode = .byTruncatingTail
noDataLbl?.center = self.view.center
view.addSubview(noDataLbl!)

Upvotes: 3

devbot10
devbot10

Reputation: 1253

SWIFT 4

This worked for me and seems more future proof. This also works for a multi-line label.

override func loadView() {
    self.view = UIView()

    let message = UILabel()
    message.text = "This is a test message that should be centered."
    message.translatesAutoresizingMaskIntoConstraints = false
    message.lineBreakMode = .byWordWrapping
    message.numberOfLines = 0
    message.textAlignment = .center

    self.view.addSubview(message)

    message.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
    message.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    message.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
}

Upvotes: 11

8HP8
8HP8

Reputation: 1908

Auto Layout

To make your app future proof rather use auto layout anchors instead of setting the frame.

1. Disable translatesAutoresizing

titleLabel.translatesAutoresizingMaskIntoConstraints = false

2. Add CenterX & CenterY constraints

titleLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true

titleLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true

3. Set UILabel's text alignment to center

titleLabel.textAlignment = .center

Preview

Upvotes: 39

Rashwan L
Rashwan L

Reputation: 38843

To center a UILabel just add this row

x and y:

title.center = self.view.center

x:

title.center.x = self.view.center.x

y:

title.center.y = self.view.center.y

Upvotes: 80

Related Questions