16

I have taken UIlabel which are generated dynamically using for loop, each type diff text is assign in label, I want to give UILabel size dynamically depending on text.

Is there any easy solution in to do that in Swift?

Cœur
  • 32,421
  • 21
  • 173
  • 232
Sandesh Sardar
  • 1,431
  • 5
  • 23
  • 43

8 Answers8

11
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()

If you want to set numberOfLines according to the content of text,give your maximum lines.That is very important here.

user3182143
  • 8,953
  • 3
  • 25
  • 34
5

get a label height depending on it's text, font, and width you assign to it:

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!
bluenowhere
  • 2,373
  • 4
  • 22
  • 34
5
    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)
Piyush Sanepara
  • 1,397
  • 1
  • 17
  • 21
2
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)
Raju Abe
  • 486
  • 2
  • 9
  • 20
D.l.Prasad
  • 81
  • 1
  • 2
2

Create Extension to calculate the height of label following method return height of the label

 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)
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)

You can use Auto Layout in code. See Auto Layout Guide

NSDeveloper
  • 1,572
  • 13
  • 22
1

The Swift 4.1 extension method to calculate label height:

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
    }

}

Refer: Adjust UILabel height to text

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

Use this method to get determine the size for you string. Put maximum height and maximum width in CGSize(widhth,height) and it will return CGSize object containing height and width. Use it according with your scenario

Jean-François Corbett
  • 34,562
  • 26
  • 126
  • 176