我在 Swift 中有一个以编程方式创建的标签。无论设备的屏幕尺寸如何,我都试图将此标签定位在 x 轴的中间。我该怎么做?
var label = UILabel(frame: CGRectMake(0, 0, 400, 50))
label.textColor = UIColor.whiteColor()
label.font = UIFont(name: label.font.fontName, size: 50)
label.textAlignment = NSTextAlignment.Center
label.text = "My label is cool"
self.view?.addSubview(label)
最佳答案
使用 UIScreen.mainScreen().bounds.width 和 height 如下:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let label = UILabel(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height))
label.textColor = UIColor.blackColor()
label.font = UIFont(name: label.font.fontName, size: 50)
label.textAlignment = NSTextAlignment.Center
label.text = "My label is cool"
self.view?.addSubview(label)
}
关于xcode - 找到 X 轴的中间以放置标签 Swift Xcode 6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27466428/