CNLivePlayletHomePageHeaderCell.swift 6.72 KB
//
//  CNLivePlayletHomePageHeaderCell.swift
//  metaCode
//
//  Created by zx on 2024/1/4.
//  短剧主页-头视图Cell

import Foundation
import QMUIKit
import SwiftUI
import SnapKit
class CNLivePlayletHomePageHeaderCell:UITableViewCell{
    //MARK: - 懒加载
    lazy var iconImgView :UIImageView = {
        let iconImgView = UIImageView(frame: CGRect(x: 20, y: 20, width: 112, height: 147))
        return iconImgView
    }()
    ///标题
    lazy var titleLabl: UILabel = {
        let titleLabel = UILabel.init(frame: CGRectMake(self.iconImgView.right+15, self.iconImgView.top, KSreenWidth-(self.iconImgView.right+15)-10, 21))
        titleLabel.textColor = HexColor("#333333")
        titleLabel.textAlignment = .left
        titleLabel.font = Font_15
        titleLabel.text = "啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊"
        let height = titleLabel.qmui_lineHeight
        titleLabel.height = height
        return titleLabel
    }()
    ///集数
    lazy var jishuLabel: UILabel = {
        let titleLabel = UILabel.init(frame: CGRectMake(self.titleLabl.left, self.titleLabl.bottom+20, 40, 21))
        titleLabel.textColor = HexColor("#333333")
        titleLabel.textAlignment = .left
        titleLabel.font = Font_15
        titleLabel.text = "45集"
        let height = titleLabel.qmui_lineHeight
        titleLabel.height = height
        return titleLabel
    }()
    ///简介
    lazy var jianjieLabel: UILabel = {
        let titleLabel = UILabel.init(frame: CGRectMake(self.iconImgView.left, self.iconImgView.bottom+10, KSreenWidth-2*self.iconImgView.left, 21))
        titleLabel.textColor = HexColor("#333333")
        titleLabel.textAlignment = .left
        titleLabel.font = Font_14
        titleLabel.text = "啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈啊哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈"
        titleLabel.numberOfLines = 0
        var lines = titleLabel.getRealLabelTextLines()
        let height = titleLabel.qmui_lineHeight
        if lines > 2 {
            lines = 2
        }
        titleLabel.height = height*CGFloat(lines)
        return titleLabel
    }()
    //展开,收起
    lazy var expandBtn: UIButton = {
        let expandBtn = UIButton.init(type: .custom)
        expandBtn.backgroundColor = .white
        
        expandBtn.frame = CGRect(x: self.jianjieLabel.right-60, y: self.jianjieLabel.bottom, width: 60, height: 30)
        expandBtn.setTitle("展开", for: .normal)
        expandBtn.setTitle("收起", for: .selected)

        expandBtn.setTitleColor(HexColor("#333333"), for: .normal)
        expandBtn.setImage(UIImage(named: "DS_coupon_unSelect"), for: .normal)
        expandBtn.setImage(UIImage(named: "DS_coupon_unSelect"), for: .selected)
        expandBtn.titleLabel?.font = Font_16
        expandBtn.setupButtonImageAndTitlePossitionWith(padding: 5, style: .right)
        expandBtn.addTarget(self, action: #selector(expandBtnAction), for: .touchUpInside)
        return expandBtn
    }()
    //追剧|已追剧
    lazy var followBtn: UIButton = {
        let  followBtn = UIButton(type: .custom)
        followBtn.setTitle("追剧", for: .normal)
        followBtn.setTitleColor(.black, for: .normal)
        followBtn.setTitleColor(.black, for: .selected)
        followBtn.setTitle("已追剧", for: .selected)
        followBtn.layer.borderColor = UIColor.black.cgColor
        followBtn.layer.borderWidth = 1
        followBtn.layer.cornerRadius = 3
        followBtn.clipsToBounds = true
        followBtn.addTarget(self, action: #selector(followBtnAction), for: .touchUpInside)
        return followBtn
    }()
    //付费解锁全集|已购买
    lazy var paymentBtn: UIButton = {
        let  paymentBtn = UIButton(type: .custom)
        paymentBtn.setTitle("付费解锁全集", for: .normal)
        paymentBtn.setTitleColor(.white, for: .normal)
        paymentBtn.setTitle("已购买", for: .selected)
        paymentBtn.setTitleColor(.black, for: .selected)
        paymentBtn.backgroundColor = .blue
        paymentBtn.layer.borderColor = UIColor.black.cgColor
        paymentBtn.layer.borderWidth = 1
        paymentBtn.layer.cornerRadius = 3
        paymentBtn.clipsToBounds = true
        paymentBtn.addTarget(self, action: #selector(paymentBtnAction), for: .touchUpInside)
        return paymentBtn
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        self.setUI()
    }
    
    func setUI(){
        self.contentView.addSubview(self.iconImgView)
        self.contentView.addSubview(self.titleLabl)
        self.contentView.addSubview(self.jishuLabel)
        self.contentView.addSubview(self.jianjieLabel)
        self.contentView.addSubview(self.expandBtn)
        self.contentView.addSubview(self.followBtn)
        self.contentView.addSubview(self.paymentBtn)
        
        let lines = jianjieLabel.getRealLabelTextLines()
        if lines <= 2{
            self.expandBtn.isHidden = true
        }else{
            self.expandBtn.isHidden = false
        }
        
        self.followBtn.snp.makeConstraints { make in
            make.top.equalTo(self.jianjieLabel.snp.bottom).offset(40)
            make.left.equalTo(self.jianjieLabel.snp.left).offset(30)
            make.width.equalTo(120)
            make.height.equalTo(30)
        }
        self.paymentBtn.snp.makeConstraints { make in
            make.top.equalTo(self.followBtn)
            make.left.equalTo(self.followBtn.snp.left).offset(KSreenWidth/2)
            make.width.equalTo(120)
            make.height.equalTo(30)
        }
        
        self.iconImgView.backgroundColor = .red
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - Action
    @objc func expandBtnAction(button:UIButton){
        button.isSelected = !button.isSelected
        let lines = jianjieLabel.getRealLabelTextLines()
        let height = jianjieLabel.qmui_lineHeight

        if button.isSelected{
            //文本收起状态,点击展开
            self.jianjieLabel.height = CGFloat(lines)*height
        }else{
            //文本展开状态,点击收起
            self.jianjieLabel.height = 2*height
        }
        self.expandBtn.top = self.jianjieLabel.bottom

    }
    @objc func followBtnAction(button:UIButton){
        button.isSelected = !button.isSelected

    }
    @objc func paymentBtnAction(button:UIButton){
        button.isSelected = !button.isSelected

    }
    
}