pod 'AttributedString'

  - AttributedString (3.4.1)

 

           subTitleLabel.attributed.text = """
                \("蓝牙连接失败,点击 ", .foreground(UIColor.gray))\("查看教程", .link("查看教程"), .foreground(UIColor.red), .action { [weak self] in
                self?.jumpTutorial()
            })
            """

 

            lbl.attributed.text = """
                \("《隐私协议》", .link("《隐私协议》"), .foreground(.hx_color(withHexStr: "#FD8E50")), .action {
                    self.didTouchAction?(.privateProtocl)
                })\("《用户服务协议》", .link("《用户服务协议》"), .foreground(.hx_color(withHexStr: "#FD8E50")), .action { self.didTouchAction?(.userProtocol) })
            """

 

 

多行的时候需要计算高度 最后 +20,不然显示不全

        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


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

 

 

==============================================================================================================

第二种使用YYLabel

 

1.在桥接文件 添加引用

文件-Bridging-Header.h
添加

#import <YYKit/YYLabel.h> ///富文本点击

 

 

下边使用

        // 设置行间距(可根据需要调整)第一种

        let paragraphStyle = NSMutableParagraphStyle()

      paragraphStyle.center = .left //文字居左

           paragraphStyle.lineSpacing = 4 //上下间隔4

           let attributedString = NSMutableAttributedString(string: fullStr)

        attStr.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: fullStr.count))


let str = "详情请查看《隐私协议》与《用户服务协议》" let fullStrColor = AppTheme.theme.isDark ? UIColor.yd.hex(0xFFFFFF):UIColor.yd.hex(0x242942) let attStr = NSMutableAttributedString(string: str, attributes: [ .foregroundColor: fullStrColor, .font: UIFont.yd.footnoteRegular,

         .paragraphStyle: paragraphStyle ///设置行间距(可根据需要调整)第二种

         ])
        let privacyRange = (str as NSString).range(of: "《隐私协议》")
        attStr.setTextHighlight(privacyRange, color: UIColor.yd.hex(0xFF9659), backgroundColor: nil) { [weak self] (containerView, text, range, rect) in
            self?.jumpH5(title: "隐私政策", urlStr: "privacy-protocol")
        }
        
        let serviceRange = (str as NSString).range(of: "《用户服务协议》")
        attStr.setTextHighlight(serviceRange, color: UIColor.yd.hex(0xFF9659), backgroundColor: nil) { [weak self] (containerView, text, range, rect) in
            self?.jumpH5(title: "用户服务协议", urlStr: "serve-protocol")
        }
        checkLab.attributedText = attStr 

 

OC使用

#pragma mark - Private method
- (void)setHighlightWithPrivacy:(NSString *)privacyAgreement andService:(NSString *)serviceAgreement {
    if (_contentStr) {
        NSString *descString = _contentStr;
        NSMutableAttributedString *mutaAttributeString = [[NSMutableAttributedString alloc] initWithString:descString attributes:@{NSForegroundColorAttributeName:[YDColor hex:@"#565656"], NSFontAttributeName: FONT_SIZE(14)}];
        
        // 创建段落样式
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineSpacing = 3; // 设置换行间距值
        
        // 将段落样式应用到 attributedString
        [mutaAttributeString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, mutaAttributeString.length)];
        mutaAttributeString.alignment = NSTextAlignmentCenter;
        [mutaAttributeString setTextHighlightRange:[descString rangeOfString:privacyAgreement] color:YDColor.mainColor backgroundColor:nil tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            if (self.indexBlock) {
                self.indexBlock(3, @"");
            }
        }];
        
        [mutaAttributeString setTextHighlightRange:[descString rangeOfString:serviceAgreement] color:YDColor.mainColor backgroundColor:nil tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            if (self.indexBlock) {
                self.indexBlock(2, @"");
            }
        }];
        
        self.contentLb.attributedText = mutaAttributeString;
    }
}

 

posted on 2025-03-26 17:14  懂事长qingzZ  阅读(112)  评论(0)    收藏  举报