首页 > 解决方案 > 如何将描述上的标签高度添加到卡片高度?

问题描述

在此处输入图像描述1. 这是我的代码的结果。


在此处输入图像描述

这是我的约束的布局图片。


// * 只是为了让您知道我是 ios 开发的新手,所以我在布局和约束方面//一直在苦苦挣扎,所以如果你们中的任何人//可以帮助我,我会很高兴 *

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

在此处输入图像描述

标签: iosswiftswift4

解决方案


您可以像这样计算标签大小:

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

推荐阅读