【问题标题】:How to center UILabel in Swift?如何在 Swift 中将 UILabel 居中?
【发布时间】:2016-04-11 07:11:57
【问题描述】:

我正在尝试使某些文本居中,但它似乎不起作用。

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.
    }

}

这是我正在使用的代码,但这是我得到的:

它不在屏幕的中心。谁能告诉我我做错了什么?

【问题讨论】:

    标签: ios swift uikit uilabel


    【解决方案1】:

    要使 UILabel 居中,只需添加这一行

    x 和 y:

    title.center = self.view.center
    

    x:

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

    y:

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

    【讨论】:

    • 那行得通!但是如果我只想把它放在 X 轴的中心呢?
    • 只需添加 .x title.center.x = self.view.center.xfor y: title.center.y = self.view.center.y
    • 我正在通过 tableView viewfForHeaderInSection 的委托方法自定义我的标题视图,它不像你提到的那样将标签居中。我正在设置像 label.frame = CGRect(x: self.view.frame.size.width/2, y: self.view.bounds.height/2, width: 80, height: 20) 这样的框架,我已经在它之前添加你的行。
    • @UsamabinAttique,如果我理解您的问题,请尝试设置 tableView 的中心。 tableView.center.ytableView.center.x
    • 是的,它是一个表格视图,但我正在通过我提到的方法自定义标题。如果您不自定义标题,则可以只返回高度和要在其中显示的任何字符串。在我的情况下,它仍然是一个标签,但视图有点大,配色方案不同,并且委托方法返回一个 UIView,所以我首先像 let view = UIView() 一样初始化它,然后以编程方式在其中设置标签。它不工作
    【解决方案2】:

    自动布局

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

    1。禁用 translatesAutoresizing

    titleLabel.translatesAutoresizingMaskIntoConstraints = false
    

    2。添加 CenterX 和 CenterY 约束

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

    3。将 UILabel 的文本对齐设置为居中

    titleLabel.textAlignment = .center
    

    【讨论】:

    • 这是最好和更安全的答案。由于 iOS 9 仅支持此功能,因此对于较低版本,您可以使用:NSLayoutConstraint(item: titleLabel, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1, constant: 0).isActive = trueNSLayoutConstraint(item: titleLabel, attribute: .centerY, relatedBy: .equal, toItem: view, attribute: .centerY, multiplier: 1, constant: 0).isActive = true
    【解决方案3】:

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

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

    如果你想水平居中,你可以这样做:

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

    此外,如果您想将标签的 x 和 y 值居中,您可以这样做:

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

    【讨论】:

    • 我试过这个,但我收到错误消息“UILABEL 类型的值没有成员'位置'”
    【解决方案4】:

    SWIFT 4

    这对我有用,而且似乎更有前途。这也适用于多行标签。

    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
    }
    

    【讨论】:

      【解决方案5】:

      swift 3/4/5 的代码

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-07
        • 2015-05-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多