2016-12-12 41 views
0
contentLabel.preferredMaxLayoutWidth = kScreenWidth - 20 
contentLabel.numberOfLines = 0 

我使用SnapKit設置頂部和左側。爲什麼標籤的高度爲零?

contentLabel.snp.makeConstraints { (make) in 
    make.top.equalTo(topView.snp.bottom) 
    make.left.equalTo(topView.iconView) 
} 
print(contentLabel.frame.size.height) 

它打印0.它沒事嗎?

回答

1

您可以通過此功能在斯威夫特3獲得標籤高度:

func GetheightOfLable(strLable:String , widthlable:CGFloat) -> CGFloat{ 

    let rectDetail:CGRect = strLable.boundingRect(with: CGSize(width: widthlable, height: CGFloat(MAXFLOAT)), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSFontAttributeName:UIFont.systemFontSize], context: nil) 
    print(rectDetail.height) 
    return rectDetail.height 
} 
0

你可以試試這個,我認爲這將幫助你

let sizeToFit = CGSize(width: contentLabel.frame.size.width, 
         height: CGFloat.greatestFiniteMagnitude) 
let textSize = contentLabel.sizeThatFits(sizeToFit) 

希望這將有助於你

相關問題