-1
class MojoCell :UITableViewCell{ // cell for Mojo    

@IBOutlet weak var height: NSLayoutConstraint!
@IBOutlet weak var qNumber: UILabel!
@IBOutlet weak var question: UILabel!
@IBOutlet weak var answer: UISegmentedControl!

func configure(_ mojo: MojoModel) {

    self.qNumber.text = mojo.questionNumber

    let text :String = "123456789-123456789-123456789-1234567889-123456789"
    self.question.sizeToFit()
    question.numberOfLines = 0
    question.lineBreakMode = NSLineBreakMode.byWordWrapping
    question.text = text
    question.sizeToFit()      
}

I used the above code to adjust the height of the label according to the content of the UIlabel. But It does not work I expected. It only shows the portion of the text.

How It looks lie on X-CODE How It looks lie on Simulator

AtulParmar
  • 4,358
  • 1
  • 24
  • 45
Kasun
  • 21
  • 5
  • Possible duplicate of [Dynamically increase height of UILabel & TableView Cell?](https://stackoverflow.com/questions/36052361/dynamically-increase-height-of-uilabel-tableview-cell) – Vivek Apr 10 '19 at 04:07

2 Answers2

0

UITableView have automaticDimension. It will make your row dynamic, you don't need to give heightForRowAt.

You need to put estimatedRow height = "put your row height". Then in rowHeight = UITableView.automaticDimension

Try this in your code it'll work for you. If you don't understand above code, then try this:

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }

    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return "put your estimated height for row"
    }
Habin Lama
  • 529
  • 5
  • 19
0

Use Below code in func configure(_ mojo: MojoModel)

 templabel.text = "Your text here" <br/>
 templabel.textAlignment = .center <br/>
 templabel.frame = CGRect(x:0,y:0,width:templabel.intrinsicContentSize.width,height:templabel.intrinsicContentSize.width)
Bhavesh Nayi
  • 705
  • 4
  • 15
Jack Patel
  • 21
  • 4