iOS用户协议界面实现方案(YYText实现部分文字的高亮点击)

iOS用户协议界面实现方案(YYText实现部分文字的高亮点击)

通常情况下注册、登录界面下方都会包含用户协议和隐私政策的界面如下:


image1

实现思路

  1. 左侧选中框是个check按钮
  2. 阅读并同意用户协议和隐私政策部分是富文本
  3. 给富文本中的“用户协议”、“隐私政策”添加点击事件

实现方案

1.  继承UIView创建一个独立视图方便复用
2.  在视图上添加check按钮
3.  用YYText实现富文本和点击事件

YYText

实现代码

/// 用户协议视图
class UTUserAgreementView: UIView {

    /// 协议类型
    ///
    /// - user: 用户协议
    /// - privacy: 隐私政策
    enum agreementType{
        case user
        case privacy
    }
    
    /// 选中事件
    let selectPS = PublishSubject()
    //界面高20 款240
    /// 核查选择框
    lazy var checkBox: UIButton = {
        let view = UIButton()
        view.setImage(UIImage.init(named: "login_weigouxuan"), for: UIControl.State.normal)
        view.setImage(UIImage.init(named: "login_gouxuan"), for: UIControl.State.selected)
        view.addTarget(self, action: #selector(checkBoxAction(sender:)), for: .touchUpInside)
        view.isSelected = true
        self.addSubview(view)
        return view
    }()
    
    /// 协议文本
    lazy var agreeMentLB: YYLabel  = {
        //创建YYLabel
        let view = YYLabel()
        //创建富文本,设置默认font和color
        let text = NSMutableAttributedString.init(string: "阅读并同意用户协议和隐私政策", attributes: [.font : UIFont.systemFont(ofSize: 14)])
        text.yy_color = UIColor.UT79Color()
        let decoration = YYTextDecoration.init(style: YYTextLineStyle.single, width: 1, color: UIColor.UT79Color())
        //第一个特殊显示区域,设置对应的font和color
        let rangeOne = NSRange.init(location: 5, length: 4)
        text.yy_setTextHighlight(rangeOne, color: UIColor.UT20Color(), backgroundColor: UIColor.clear, tapAction: { (view, string, range, rect) in
            //点击事件回调
            self.selectPS.onNext(.user)
        })
        //第二个特殊显示区域,设置对应的font和color
        let rangeTwo = NSRange.init(location: 10, length: 4)
        text.yy_setTextHighlight(rangeTwo, color: UIColor.black, backgroundColor: UIColor.clear, tapAction: { (view, string, range, rect) in
            //点击事件回调
            self.selectPS.onNext(.privacy)
        })
        text.yy_setTextUnderline(decoration, range: rangeOne)
        text.yy_setTextUnderline(decoration, range: rangeTwo)
        //加载富文本
        view.attributedText = text
        self.addSubview(view)
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.clear
        deploySubviews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func deploySubviews(){
        agreeMentLB.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview().offset(12.5)
            make.top.bottom.equalToSuperview()
        }
        
        checkBox.snp.makeConstraints { (make) in
            make.centerY.equalTo(agreeMentLB.snp.centerY).offset(-0.5)
            make.width.height.equalTo(20)
            make.right.equalTo(agreeMentLB.snp.left).offset(-5)
        }
    }
    
    /// 核验按钮
    ///
    /// - Parameter sender: 按钮对象
    @objc private func checkBoxAction(sender: UIButton){
        sender.isSelected = !sender.isSelected
    }
}

你可能感兴趣的:(iOS用户协议界面实现方案(YYText实现部分文字的高亮点击))