UIlabel 的 textaligment 不起作用

UIlabel's textaligment doesn't work

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    // create the parent view that will hold header Label
    var customView : UIView = UIView()
    var a = screen_h/13+screen_h/30
    var b = screen_h-(screen_h/13+screen_h/30+1 + screen_h*0.8)
    customView.frame = CGRectMake(0.0, 0.0, screen_w, (screen_h - a - b )/5)

    var headerLabel : UILabel = UILabel()

    headerLabel.backgroundColor = UIColor.grayColor()
    headerLabel.opaque = false
    headerLabel.textColor = UIColor.whiteColor()
    headerLabel.highlightedTextColor = UIColor.whiteColor()
    headerLabel.font = headerLabel.font.fontWithSize(30)
    headerLabel.textAlignment == NSTextAlignment.Right
    headerLabel.frame = CGRectMake(0.0, 0.0, screen_w, (screen_h - a - b )/5)
    headerLabel.layer.borderWidth = 5
    headerLabel.layer.borderColor = UIColor.blackColor().CGColor
    headerLabel.text = "My Header"; // i.e. array element

    customView.addSubview(headerLabel)

    return customView
}

在此代码中,我想让我的 UILabel 文本居中,但文本对齐不起作用。

您似乎在赋值运算符中输入错误,并分配了错误的文本对齐方式。

改变

headerLabel.textAlignment == NSTextAlignment.Right

headerLabel.textAlignment = NSTextAlignment.Center