代码之家  ›  专栏  ›  技术社区  ›  naga

在swift中点击textview字符串的手势

  •  0
  • naga  · 技术社区  · 5 年前

    enter image description here

        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
          }
    
    0 回复  |  直到 5 年前
        1
  •  0
  •   Amber K    5 年前

    你需要添加 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
    

        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
        }
    

    同时将isEditable设置为false,将isSelectable设置为true。子类textView并在中返回false canPerformAction . 在 awakeFromNib 设置 textDragInteraction?.isEnabled = false