如何在Swift中以编程方式为UILabel提供动态高度?

时间:2015-07-05 09:16:12

标签: ios iphone swift

我已经使用for循环动态生成的UIlabel,每个类型的diff文本都在标签中分配,我想根据文本动态地给出UILabel大小。

在Swift中有没有简单的解决方案?

8 个答案:

答案 0 :(得分:9)

let label:UILabel = UILabel(frame: CGRectMake(x, y, width, height))
label.numberOfLines = 4
label.lineBreakMode = NSLineBreakMode.ByWordWrapping
let font = UIFont(name: "Helvetica", size: 20.0)
label.font = font
label.text = "Whatever you want the text enter here"
label.sizeToFit()

如果你想根据文字内容设置numberOfLines,请给出你的最大行数。这里非常重要。

答案 1 :(得分:4)

获取标签高度,具体取决于您分配给它的文字,字体和宽度:

func rectForText(text: String, font: UIFont, maxSize: CGSize) -> CGSize {
        let attrString = NSAttributedString.init(string: text, attributes: [NSFontAttributeName:font])
        let rect = attrString.boundingRectWithSize(maxSize, options: NSStringDrawingOptions.UsesLineFragmentOrigin, context: nil)
        let size = CGSizeMake(rect.size.width, rect.size.height)
        return size
    }

let labelSize = rectForText("your text here", font: UIFont.systemFontOfSize(your font), maxSize: CGSizeMake(your label width,999))
let labelHeight = labelSize.height //here it is!

答案 2 :(得分:4)

    myLabel.text = "Your Label Text Here"

    myLabel.textAlignment = .Natural

    myLabel.numberOfLines = 0

    myLabel.sizeToFit()

    myLabel.frame = CGRectMake(myLabel.frame.origin.x, myLabel.frame.origin.y, 280, myLabel.frame.height)

答案 3 :(得分:1)

let label = UILabel()
label.backgroundColor = UIColor.greenColor()
label.text = "Hello,world.\n Just a test."
let font = UIFont.systemFontOfSize(17.0)
label.font = font
label.numberOfLines = 0;
let text = label.text! as NSString
let size = text.sizeWithAttributes([NSFontAttributeName:font])
label.frame = CGRectMake(0, 0, size.width, size.height)

您可以在代码中使用自动布局。 See Auto Layout Guide

答案 4 :(得分:1)

let label:UILabel = UILabel()

                                        label.textColor=UIColor.black
                                        label.font = UIFont(name: "Halvetica", size: 17)
                                        label.numberOfLines = 1

                                        label.text = item.name
                                        label.sizeToFit()

                                        label.frame = CGRect(x: 5, y: imageView.frame.height+10, width: 50, height:label.frame.height)

答案 5 :(得分:1)

用于计算标签高度的 Swift 4.1 扩展方法:

extension UILabel {

    func heightForLabel(text:String, font:UIFont, width:CGFloat) -> CGFloat {
        let label:UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: width, height: CGFloat.greatestFiniteMagnitude))
        label.numberOfLines = 0
        label.lineBreakMode = NSLineBreakMode.byWordWrapping
        label.font = font
        label.text = text

        label.sizeToFit()
        return label.frame.height
    }

}

参考:Adjust UILabel height to text

答案 6 :(得分:0)

NSString(string: "hello this is a string").boundingRect(
        with: CGSize(width: width, height: .greatestFiniteMagnitude),
        options: .usesLineFragmentOrigin,
        attributes: [.font: self],
        context: nil).size

使用此方法确定字符串的大小。将最大高度和最大宽度放在CGSize(widhth,height)中,它将返回包含高度和宽度的CGSize对象。根据您的方案使用它

答案 7 :(得分:0)

创建扩展以根据标签的方法返回高度计算标签的高度

 import UIKit

func heightForView(text:String, font:UIFont, width:CGFloat) -> CGFloat{
 let label:UILabel = UILabel(frame: CGRectMake(0, 0, width, CGFloat.greatestFiniteMagnitude))
 label.numberOfLines = 0
 label.lineBreakMode = NSLineBreakMode.byWordWrapping
 label.font = font
 label.text = text

 label.sizeToFit()
 return label.frame.height
}

let font = UIFont(name: "Helvetica", size: 20.0)
var height = heightForView("This is just a load of text", font: font, width: 60)