修改UILabel文本属性

时间:2015-10-01 09:43:47

标签: swift uilabel

我正在尝试修改UILabel中显示的文本字符串。这是我通过代码构建它的方式:

func buildLabel() -> UILabel {
let rect = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 40)
let myview = UILabel(frame: rect)
    myview.backgroundColor = UIColor.brownColor()
    myview.numberOfLines = 0
    myview.lineBreakMode = NSLineBreakMode.ByWordWrapping
    myview.sizeToFit()

    return myview
}

然后在另一种方法中,我试图简单回忆一下:

buildLabel().text = "whatever I put inside here"

但字符串永远不会出现。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

这样做。

self.view.addSubview(buildLabel("whatever I put inside here"))

func buildLabel(text : String) -> UILabel {
    let rect = CGRect(x: 0, y: 100, width: self.view.frame.size.width, height: 40)
    let myview = UILabel(frame: rect)
    myview.backgroundColor = UIColor.brownColor()
    myview.numberOfLines = 0
    myview.text = text;
    myview.lineBreakMode = NSLineBreakMode.ByWordWrapping
    myview.sizeToFit()

    return myview
}