I ended up doing the following;
Created a UIButton (empty text label) for my actual checkbox and a UITextView for my adjacent checkbox label text.
I then used NSMutableAttributedString
like so;
class MyViewController: UIViewController, UITextViewDelegate {
override func viewDidLoad() {
let attributedTermsTitle = NSMutableAttributedString()
attributedTermsTitle.append(NSAttributedString(string: "I have read and accept the ", attributes: [NSFontAttributeName:UIFont(name: "My-Font", size: 14)!]))
attributedTermsTitle.append(NSAttributedString(string: "Terms & Conditions", attributes: [NSFontAttributeName:UIFont(name: "My-Font", size: 14)!, NSLinkAttributeName: "terms"]))
termsText.linkTextAttributes = [NSForegroundColorAttributeName: UIColor.blue]
termsText.attributedText = attributedTermsTitle
termsText.delegate = self
termsText.isSelectable = true
termsText.isUserInteractionEnabled = true
termsText.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(MyViewController.toggleCheckbox)))
}
func toggleCheckbox(sender: UITapGestureRecognizer) {
termsButton.isChecked = !termsButton.isChecked
}
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
let link = URL.absoluteString
if(link == "terms") {
// link to the Terms web page / screen
}
return false
}
}
and for the UIButton, gave the class;
class CheckBox: UIButton {
let checkedImage = UIImage(named: "checkbox_on")! as UIImage
let uncheckedImage = UIImage(named: "checkbox_off")! as UIImage
var isChecked: Bool = false {
didSet{
if isChecked == true {
self.setImage(checkedImage, for: UIControlState.normal)
} else {
self.setImage(uncheckedImage, for: UIControlState.normal)
}
}
}
override func awakeFromNib() {
self.addTarget(self, action:#selector(buttonClicked(sender:)), for: UIControlEvents.touchUpInside)
self.isChecked = false
}
func buttonClicked(sender: UIButton) {
if sender == self {
isChecked = !isChecked
}
}
}
End result is that both the UIButton and the non-linked text of the UITextView will toggle the checkbox on and off. The "Terms & Conditions" text will link to the website or segue somewhere else.
Hopefully this helps someone else out.