Change the color of a link in an NSMutableAttributedString

后端 未结 5 1747
谎友^
谎友^ 2020-12-13 08:15

I have the following code but my links are always blue. How do I cange the color of them?

[_string addAttribute:NSLinkAttributeName value:tag range:NSMakeRan         


        
5条回答
  •  猫巷女王i
    2020-12-13 08:50

    For swift3.0

      override func viewDidLoad() {
         super.viewDidLoad()
    
      let linkAttributes = [
            NSLinkAttributeName: NSURL(string: "http://stalwartitsolution.co.in/luminutri_flow/terms-condition")!
            ] as [String : Any]
      let attributedString = NSMutableAttributedString(string: "Please tick box to confirm you agree to our Terms & Conditions, Privacy Policy, Disclaimer. ")
    
      attributedString.setAttributes(linkAttributes, range: NSMakeRange(44, 18))
    
      attributedString.addAttribute(NSUnderlineStyleAttributeName, value: NSNumber(value: 1), range: NSMakeRange(44, 18))
    
      textview.delegate = self
      textview.attributedText = attributedString
      textview.linkTextAttributes = [NSForegroundColorAttributeName: UIColor.red]
      textview.textColor = UIColor.white
      }
    
    
      func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
        return true
       }
    

提交回复
热议问题