ios 如何在 Swift 中居中 UILabel?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34645943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:32:16  来源:igfitidea点击:

How to center UILabel in Swift?

iosswiftuikituilabel

提问by elpita

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?

它不是屏幕的中心。有人能告诉我我做错了什么吗?

回答by Rashwan L

To center a UILabel just add this row

要居中 UILabel 只需添加这一行

x and y:

x 和 y:

title.center = self.view.center

x:

X:

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

y:

y:

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

回答by 8HP8

Auto Layout

自动布局

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

为了使您的应用程序面向未来,请使用自动布局锚点而不是设置框架。

1. Disable translatesAutoresizing

1. 禁用 translatesAutoresizing

titleLabel.translatesAutoresizingMaskIntoConstraints = false

2. Add CenterX & CenterY constraints

2. 添加CenterX & CenterY 约束

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

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

3. Set UILabel's text alignment to center

3.设置UILabel的文字对齐居中

titleLabel.textAlignment = .center

Preview

预览

回答by Charles-olivier Demers

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:

实际上,您正在做的是将 UILabel 内的文本居中。您要做的是将标签居中。要做到这一点,你可以这样做:

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:

此外,如果您想将标签的 x 和 y 值居中,您可以执行以下操作:

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

回答by Devbot10

SWIFT 4

快速 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
}

回答by vilas deshmukh

Code for swift 3/4/5

swift 3/4/5 的代码

Paste below code after super.viewDidLoad()

在 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!)