jeudi 19 mai 2022

Checkbox with links in text are not working

I have created a custom checkbox in Swift which has a textlabel with an underlined link in it.

The link is perfectly underlined in the textlabel and the link is working fine.

But... the link only works when I do a long tap on the underlined part, and if I just do a single tap it's selecting/unselecting the checkbox.

I want that only the square part is checked/unchecked with a single tap and the textlabel is opening the link on a single tap when a link is available.

Is this possible?

override func setupCheckBox() {
   self.addSubview(horizontalStackView)
    self.horizontalStackView.anchor(top: topAnchor, leading: leadingAnchor, bottom: bottomAnchor, trailing: trailingAnchor, padding: UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0))
    
    let containerCheckImage = UIView()
    containerCheckImage.translatesAutoresizingMaskIntoConstraints = false
    containerCheckImage.addSubview(checkImage)
    self.horizontalStackView.addArrangedSubview(containerCheckImage)
    self.horizontalStackView.addArrangedSubview(textView)
    
    NSLayoutConstraint.activate([
        self.checkImage.widthAnchor.constraint(equalToConstant: CheckBoxView.checkSize),
        self.checkImage.heightAnchor.constraint(equalToConstant: CheckBoxView.checkSize),
        self.checkImage.leadingAnchor.constraint(equalTo: containerCheckImage.leadingAnchor),
        self.checkImage.centerYAnchor.constraint(equalTo: containerCheckImage.centerYAnchor),
        containerCheckImage.topAnchor.constraint(equalTo: self.horizontalStackView.topAnchor),
        containerCheckImage.bottomAnchor.constraint(equalTo: self.horizontalStackView.bottomAnchor),
        containerCheckImage.widthAnchor.constraint(equalToConstant: CheckBoxView.checkSize),
        containerCheckImage.leadingAnchor.constraint(equalTo: self.horizontalStackView.leadingAnchor),
        self.textView.topAnchor.constraint(equalTo: self.horizontalStackView.topAnchor),
        self.textView.bottomAnchor.constraint(equalTo: self.horizontalStackView.bottomAnchor)
    ])
    
    self.isUserInteractionEnabled = true
    self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(onCheckBoxTapped)))
}

@objc override func setupColors() {
    let isDarkMode = AppDelegate.isDarkMode
    self.textView.textColor = isDarkMode ? .white : fontColor
}

override func setText(_ text: String?) {
    guard let text = text else {
        return
    }
    
    let font = Font.regular.font(withSize: 14)
    let decoder = HTMLParser.make(withFont: font)
    let isDarkMode = AppDelegate.isDarkMode
    textView.attributedText = decoder.parseHTMLString(string: text)
    textView.linkTextAttributes = [NSAttributedString.Key.underlineStyle: 1, NSAttributedString.Key.backgroundColor: UIColor.clear, NSAttributedString.Key.foregroundColor: isDarkMode ? UIColor.white : UIColor.black]
}

override func setTintColor(_ color: UIColor) {
    self.textView.tintColor = color
}

@objc override func onCheckBoxTapped(_ sender: Any) {
    if checkBoxData != nil {
        selected = !selected
        checkBoxData?.selected = selected
        checkBoxDelegate?.checkboxSelected(selected: selected)
        selectedClosure?(selected)
    }
}



Aucun commentaire:

Enregistrer un commentaire