ios - 如何使用 Swift 以编程方式创建 UILabel?

标签 ios swift xcode cocoa-touch uilabel

如何在 Xcode 6 中使用 Swift 以编程方式创建 UILabel

我已经开始在 Xcode 6 中创建一个新的“单一 View 应用程序”,并为这个项目选择了 Swift。我有我的文件 AppDelegate.swiftViewController.swift,但我不确定从这里开始做什么。

最佳答案

在 Swift 3+ 中以编程方式创建 UILabel:

override func viewDidLoad() {
    super.viewDidLoad()

    let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
    label.center = CGPoint(x: 160, y: 285)
    label.textAlignment = .center
    label.text = "I'm a test label"

    self.view.addSubview(label)
}

关于ios - 如何使用 Swift 以编程方式创建 UILabel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24081731/

相关文章:

ios - 苹果使用哪种字体/字体大小作为 iOS 中的占位符

swift - Xcode 8.2 中的 xib 文件无法正常工作。这是一个错误吗?

ios - Swift 编译器错误 : "Expression too complex" on a string concatenation

iphone - Objective-C : How do I draw lines with alpha in quad curve

ios - 如何从 SkView 中放松

ios - 记录到文件时NSString崩溃

ios - 删除表格 View 中包含数组数据的行

ios - 服务器应如何代表 iOS 用户进行身份验证?

ios - UIButton 添加没有发件人的目标

macos - Swift 弱委托(delegate)运行时错误(访问错误)。漏洞?