简体   繁体   English

快速查看textview字符串的tapgesture

[英]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, 当我单击它们时,我正在使用textview控制器显示内容,其中任何一个都显示另一个视图控制器,我尝试了此代码,但无法正常工作,

在此输入图像描述

    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 您需要添加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. 还要将isEditable设置为false并将isSelectable设置为true。 Subclass textView and return false in canPerformAction . 子类化textView并在canPerformAction返回false。 In awakeFromNib set textDragInteraction?.isEnabled = false awakeFromNib设置textDragInteraction?.isEnabled = false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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