How to center UILabel in Swift?
64,839
Solution 1
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
Solution 2
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
Solution 3
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)
Solution 4
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
}
Solution 5
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!)
Author by
elpita
Updated on February 18, 2020Comments
-
elpita about 4 years
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:
It's not center to the screen. Can someone tell me what am I doing wrong?