注意点 label的文字要自己计算高度,不然不会换行显示

                self.nopairHeaderView.layoutIfNeeded()
                let headerHeight = self.nopairHeaderView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize).height
                self.nopairHeaderView.frame = CGRect(x: 0, y: 0, width: self.tableView.frame.width, height: headerHeight)
                self.tableView.tableHeaderView = self.nopairHeaderView

 

    private lazy var nopairHeaderView = createBleConnectNopairHeaderView()


    ///蓝牙已连接未配对 HeaderView 内部使用snapkit 布局好
    private func createBleConnectNopairHeaderView() -> UIView {
        let bgView = UIView()
        
        let titleLab = UILabel()
        titleLab.text = "蓝牙未配对,请点击配对蓝牙"
        titleLab.font = .yd.textMedium
        titleLab.textColor = R.color.text_main()
        titleLab.textAlignment = .center
        
        let subTitleLab = UILabel()
        subTitleLab.text = "一键开机依赖手机与车端蓝牙配对"
        subTitleLab.font = .yd.footnoteRegular
        subTitleLab.textColor = R.color.text_sub()
        subTitleLab.textAlignment = .center
        
        let iv = UIImageView(image: R.image.icon_topBgBleConnect_noPair())
        
        let desLab = UILabel()
        desLab.numberOfLines = 0
        desLab.textAlignment = .left
        let attrText = ASAttributedString.init(
        """
        \("请携带手机靠近车辆,点击", .font(.yd.footnoteRegular) , .foreground(R.color.text_sub()!))\("【配对蓝牙】", .font(.yd.footnoteRegular), .link("【配对蓝牙】"), .foreground(R.color.color_blue()!), .action { [weak self] in
                            guard let self = self else { return }
                            self.openBluetoothSettings()
        })\(",APP中弹出蓝牙配对提示时请选择允许。也可在系统蓝牙列表中,找到XXXX标识的蓝牙,手动点击配对",.font(.yd.footnoteRegular), .foreground(R.color.text_sub()!))
        """)
        desLab.attributed.text = attrText


        [titleLab, subTitleLab, iv, desLab]
            .forEach {
                bgView.addSubview($0)
            }
        titleLab.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.top.equalTo(20 + ydNavigationBarHeight())
        }
        subTitleLab.snp.makeConstraints { make in
            make.left.right.equalTo(titleLab)
            make.top.equalTo(titleLab.snp.bottom).offset(8)
            
        }
        iv.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(subTitleLab.snp.bottom).offset(12)
            make.width.equalTo(260)
            make.height.equalTo(105)
        }
        
        let desLabHeight = attrText.value.boundingRect(
            with: .init(
                width: SCREEN_WIDTH - 16*2,
                height: .greatestFiniteMagnitude
            ),
            options: [
                .usesLineFragmentOrigin,
                .usesFontLeading
            ],
            context: nil
        ).integral.size.height + 20
        
        desLab.snp.makeConstraints { make in
            make.left.right.equalTo(titleLab)
            make.top.equalTo(iv.snp.bottom).offset(12)
            make.bottom.equalToSuperview()
            make.height.equalTo(desLabHeight)
        }
        
        return bgView
    }

 

posted on 2025-03-28 11:10  懂事长qingzZ  阅读(31)  评论(0)    收藏  举报