UILabel sizeToFit不会拉伸到宽度

时间:2016-08-22 11:22:08

标签: ios swift uilabel sizetofit

我正在以编程方式创建UILabel,如下所示:

    hashtagLbl = UILabel(frame: CGRectMake(20, nameLbl.frame.maxY + 5,
    bounds.width-40, layerView.frame.width/3.66))
    hashtagLbl.text = "#Placeholder"
    hashtagLbl.font = UIFont.systemFontOfSize(18)
    hashtagLbl.textColor = UIColorFromRGB(0x9B9B9B)
    hashtagLbl.sizeToFit()
    hashtagLbl.numberOfLines = 0
    hashtagLbl.lineBreakMode = NSLineBreakMode.ByWordWrapping

现在我正在加载数据并将数据设置为标签,我想起了sizeToFit

        if let text = memberHashtags{
        hashtagLbl.text = text
        hashtagLbl.sizeToFit()

    }

而不是像这样显示(在IB中创建的UILabel): enter image description here

标签显示如下:

enter image description here

1 个答案:

答案 0 :(得分:2)

尝试致电

hashtagLbl.sizeToFit()

作为代码中的最后一句话