简体   繁体   中英

tapgesture for textview string in swift

I have a signup page with privacy and term&condition. I am using textview controller for show content when I was clicked any of them show another view controller, I tried this code but it's not working,

在此输入图像描述

    let signUpTermsAndPrivacyString = NSMutableAttributedString(string: "I have read and understood PayGyft Terms of Usage and Privacy Policy",attributes: [NSAttributedStringKey.font: UIFont(name: "Helvetica", size: 15.0)!,NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue): UIColor.darkGray])
        let termsString = signUpTermsAndPrivacyString.mutableString.range(of: "Terms of Usage")
        print("termsSTring",termsString)

        signUpTermsAndPrivacyString.addAttribute(.link, value: "http://gregoryadunbar.com", range: termsString)
        let privacyString = signUpTermsAndPrivacyString.mutableString.range(of: "Privacy Policy")
        signUpTermsAndPrivacyString.addAttribute(.link, value:"http://gregoryadunbar.com", range: privacyString)

 termsPrivacyPolicyTextView.attributedText = signUpTermsAndPrivacyString



    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
        if (URL.absoluteString == termsURLString) {
                   print("termsURLString")
         } else if (URL.absoluteString == privacyURLString) {

            print("privacyURLString")
            }
            return false
      }

You need to add myTextView.linkTextAttributes

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
let attributes = [NSAttributedStringKey.foregroundColor : UIColor.init(rgb: 0x646464),
                          NSAttributedStringKey.font : AppFont.getFont(fontType: .regular, ofSize: 16),
                          NSAttributedStringKey.paragraphStyle : paragraphStyle]

let attributedText = NSMutableAttributedString(string: descriptonText,
                                                       attributes: attributes)
if let range = attributedText.string.range(of: "terms & conditions") {
    let nsRange = NSRange(range, in: attributedText.string)
    attributedText.addAttributes([NSAttributedStringKey.link : "link://T&C"], range: nsRange)
}
let linkColor = UIColor.init(red: 47, green: 117, blue: 83)
let linkAttrs: [String: Any] = [NSAttributedStringKey.foregroundColor.rawValue : linkColor,
                                        NSAttributedStringKey.underlineColor.rawValue : linkColor,
                                        NSAttributedStringKey.underlineStyle.rawValue : NSUnderlineStyle.styleSingle.rawValue]
descriptionTextView.linkTextAttributes = linkAttrs
let textView = UITextView(frame: descriptionTextView.bounds)
textView.attributedText = attributedText
textView.layoutIfNeeded()
textView.sizeToFit()
let size = textView.sizeThatFits(CGSize(width: textView.frame.size.width, height: CGFloat.greatestFiniteMagnitude))
descriptionTextViewHeight.constant = size.height
descriptionTextView.attributedText = attributedText

And in delegate:

    func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
        return false
    }

    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        return false
    }

    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
        debugPrint(URL.absoluteString)
        if URL.absoluteString.contains("link://") {
            // my func call
            dismissAnimated()
        }
        return false
    }

Also set isEditable to false and isSelectable to true. Subclass textView and return false in canPerformAction . In awakeFromNib set textDragInteraction?.isEnabled = false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM