0

enter image description here 1. here is the result of my code .


enter image description here

here is a layout picture for my constraint.


// * just for you to know i am new on ios development swift so i have //been struggling a lot in layout and constraint so if any one of you //could help me i will be glad *

import UIKit

class DayTableViewCell: UITableViewCell {

    var chose:Bool=true

    @IBOutlet weak var card: UIView!
    @IBOutlet weak var main_stack: UIStackView!
    @IBOutlet weak var content_view: UIView!
    @IBOutlet weak var read_more_less_label: UILabel!
    @IBOutlet weak var title_label: UILabel!
    @IBOutlet weak var date_label: UILabel!
    @IBOutlet weak var description_labe: UILabel!
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        card.layer.cornerRadius = 5.0
        card.layer.shadowColor = UIColor.gray.cgColor
        card.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
        card.layer.shadowRadius = 12.0
        card.layer.shadowOpacity = 0.7
        // Configure the view for the selected state
        description_labe.isHidden=true

        let tap = UITapGestureRecognizer(target: self, action: #selector(DayTableViewCell.tapFunction))
        read_more_less_label.isUserInteractionEnabled = true
        read_more_less_label.addGestureRecognizer(tap)
        read_more_less_label.text=LocalizationSystem.sharedInstance.localizedStringForKey(key: "read_more", comment: "")
    }

    @objc func tapFunction(){

        if chose {
            description_labe.isHidden=false
            chose=false
            read_more_less_label.text=LocalizationSystem.sharedInstance.localizedStringForKey(key: "read_less", comment: "")

            //            card.frame = CGRect(x: 0, y: 0, width: card.frame.width+description_labe.bounds.size.width, height:card.frame.height + description_labe.bounds.size.height)
        }else{
            description_labe.isHidden=true
            chose=true
            read_more_less_label.text=LocalizationSystem.sharedInstance.localizedStringForKey(key: "read_more", comment: "")

            description_labe.sizeToFit()

            //here i want to increase the height of my card by adding the height of //my description_labe(label)
            self.card.frame = CGRect(x: 0, y: 0, width: card.frame.width+description_labe.frame.width, height:card.frame.height + heightForView(text: description_labe?.text ?? "", font: UIFont.systemFont(ofSize: 12), width: 300))


            print("labe_height=\(heightForView(text: description_labe?.text ?? "", font: UIFont.systemFont(ofSize: 12), width: 300))")
            //
            //            self.contentView.frame = CGRect(x: 0, y: 0, width: self.contentView.frame.width, height: self.contentView.frame.height + 20.0)

            //            card.frame =  CGRect(x: 0 , y: self.card.frame.height * 0.7, width: self.card.frame.width, height: self.card.frame.height * 0.3)

            //Set height
            //            let f = card.frame;      //Grab current
            //            card.frame = CGRect(x: f.origin.x, y: f.origin.y, width: f.width, height: 400);
            //


        }

    }

    //To calculate height for label based on text size and width
    func heightForView(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
    }
}

enter image description here

  • You should be using auto-layout instead of trying to calculate label heights. It's difficult to know what you are actually trying to do here... Show your layout so we can see where your labels are, and which label you want to change. – DonMag Apr 20 '20 at 13:18
  • You should use auto-layout (constraints), plus auto-height. It'll make your size calculation easier. Have a look here: [Using Auto Layout in UITableView for dynamic cell layouts..](https://stackoverflow.com/a/18746930/593709) – Adil Soomro Apr 20 '20 at 13:20
  • i add another picture of my layout – Yousef Beiruty Apr 20 '20 at 13:41

1 Answers1

0

You can calculate label size like this :

extension String {
    func labelSize(maxWidth: CGFloat) -> CGSize {
 
        let size = CGSize(width: maxWidth, height: .greatestFiniteMagnitude)
        
        let rect = self.boundingRect(with: size,
                                     options: .usesLineFragmentOrigin,
                                     context: nil).integral
        return rect.size
    }  
}