注意點 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 }
浙公網安備 33010602011771號