Change the color of a link in an NSMutableAttributedString

后端 未结 5 1744
谎友^
谎友^ 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条回答
  •  别那么骄傲
    2020-12-13 08:32

    Swift

     let str = "By using this app you agree to our Terms and Conditions and Privacy Policy"
     let attributedString = NSMutableAttributedString(string: str)
     var foundRange = attributedString.mutableString.rangeOfString("Terms and Conditions")
    
     attributedString.addAttribute(NSLinkAttributeName, value: termsAndConditionsURL, range: foundRange)
     foundRange = attributedString.mutableString.rangeOfString("Privacy Policy")
     attributedString.addAttribute(NSLinkAttributeName, value: privacyURL, range: foundRange)
     policyAndTermsTextView.attributedText = attributedString
     policyAndTermsTextView.linkTextAttributes = [NSForegroundColorAttributeName : UIColor.blueColor()]
    

提交回复
热议问题