NSAttributedStringKey.foregroundColor not working











up vote
5
down vote

favorite
2












I'm using ZSWTappableLabel and ZSWTaggedString to show links inside a label.



import ZSWTappableLabel
import ZSWTaggedString


The pod versions are:



  pod 'ZSWTappableLabel', '~> 2.0'
pod 'ZSWTaggedString/Swift', '~> 4.0'


The links used to appear in white (same color as the label) by default earlier, but after some update that happened recently (possibly a pod update or xcode version, I'm not able to pinpoint exactly what), the links have started to appear in blue. Setting NSAttributedStringKey.foregroundColor to white doesn't seem to affect anything. NSAttributedStringKey.backgroundColor does affect it, but for some reason the foregroundColor doesn't seem to have any effect.



How can I set the links in white color?



func setTermsAndPrivacyLinkLabel(){
termsAndPrivacyLabel.tapDelegate = self

let options = ZSWTaggedStringOptions()
options["link"] = .dynamic({ tagName, tagAttributes, stringAttributes in
guard let type = tagAttributes["type"] as? String else {
return [NSAttributedStringKey : Any]()
}

var foundURL: NSURL?

switch type {
case "privacy":
foundURL = NSURL(string: "(privacyUrl)")!
case "tos":
foundURL = NSURL(string: "(termsUrl)")!
default:
break
}

guard let URL = foundURL else {
return [NSAttributedStringKey : Any]()
}

return [
.tappableRegion: true,
NSAttributedStringKey.foregroundColor: UIColor.white,
NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 13.0),
.link: foundURL
]
})

let string = NSLocalizedString("By logging in, you agree to our <link type='tos'>terms</link> and <link type='privacy'>privacy</link>.", comment: "")
termsAndPrivacyLabel.attributedText = try? ZSWTaggedString(string: string).attributedString(with: options)
}

func tappableLabel(_ tappableLabel: ZSWTappableLabel, tappedAt idx: Int, withAttributes attributes: [NSAttributedStringKey : Any] = [:]) {
guard let url = attributes[.link] as? URL else {
return
}

UIApplication.shared.openURL(url)
}









share|improve this question
























  • Upgrading to their latest version, and following their example code (it's a little different than this) helped to fix this issue.
    – Prabhu
    Nov 10 at 17:44















up vote
5
down vote

favorite
2












I'm using ZSWTappableLabel and ZSWTaggedString to show links inside a label.



import ZSWTappableLabel
import ZSWTaggedString


The pod versions are:



  pod 'ZSWTappableLabel', '~> 2.0'
pod 'ZSWTaggedString/Swift', '~> 4.0'


The links used to appear in white (same color as the label) by default earlier, but after some update that happened recently (possibly a pod update or xcode version, I'm not able to pinpoint exactly what), the links have started to appear in blue. Setting NSAttributedStringKey.foregroundColor to white doesn't seem to affect anything. NSAttributedStringKey.backgroundColor does affect it, but for some reason the foregroundColor doesn't seem to have any effect.



How can I set the links in white color?



func setTermsAndPrivacyLinkLabel(){
termsAndPrivacyLabel.tapDelegate = self

let options = ZSWTaggedStringOptions()
options["link"] = .dynamic({ tagName, tagAttributes, stringAttributes in
guard let type = tagAttributes["type"] as? String else {
return [NSAttributedStringKey : Any]()
}

var foundURL: NSURL?

switch type {
case "privacy":
foundURL = NSURL(string: "(privacyUrl)")!
case "tos":
foundURL = NSURL(string: "(termsUrl)")!
default:
break
}

guard let URL = foundURL else {
return [NSAttributedStringKey : Any]()
}

return [
.tappableRegion: true,
NSAttributedStringKey.foregroundColor: UIColor.white,
NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 13.0),
.link: foundURL
]
})

let string = NSLocalizedString("By logging in, you agree to our <link type='tos'>terms</link> and <link type='privacy'>privacy</link>.", comment: "")
termsAndPrivacyLabel.attributedText = try? ZSWTaggedString(string: string).attributedString(with: options)
}

func tappableLabel(_ tappableLabel: ZSWTappableLabel, tappedAt idx: Int, withAttributes attributes: [NSAttributedStringKey : Any] = [:]) {
guard let url = attributes[.link] as? URL else {
return
}

UIApplication.shared.openURL(url)
}









share|improve this question
























  • Upgrading to their latest version, and following their example code (it's a little different than this) helped to fix this issue.
    – Prabhu
    Nov 10 at 17:44













up vote
5
down vote

favorite
2









up vote
5
down vote

favorite
2






2





I'm using ZSWTappableLabel and ZSWTaggedString to show links inside a label.



import ZSWTappableLabel
import ZSWTaggedString


The pod versions are:



  pod 'ZSWTappableLabel', '~> 2.0'
pod 'ZSWTaggedString/Swift', '~> 4.0'


The links used to appear in white (same color as the label) by default earlier, but after some update that happened recently (possibly a pod update or xcode version, I'm not able to pinpoint exactly what), the links have started to appear in blue. Setting NSAttributedStringKey.foregroundColor to white doesn't seem to affect anything. NSAttributedStringKey.backgroundColor does affect it, but for some reason the foregroundColor doesn't seem to have any effect.



How can I set the links in white color?



func setTermsAndPrivacyLinkLabel(){
termsAndPrivacyLabel.tapDelegate = self

let options = ZSWTaggedStringOptions()
options["link"] = .dynamic({ tagName, tagAttributes, stringAttributes in
guard let type = tagAttributes["type"] as? String else {
return [NSAttributedStringKey : Any]()
}

var foundURL: NSURL?

switch type {
case "privacy":
foundURL = NSURL(string: "(privacyUrl)")!
case "tos":
foundURL = NSURL(string: "(termsUrl)")!
default:
break
}

guard let URL = foundURL else {
return [NSAttributedStringKey : Any]()
}

return [
.tappableRegion: true,
NSAttributedStringKey.foregroundColor: UIColor.white,
NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 13.0),
.link: foundURL
]
})

let string = NSLocalizedString("By logging in, you agree to our <link type='tos'>terms</link> and <link type='privacy'>privacy</link>.", comment: "")
termsAndPrivacyLabel.attributedText = try? ZSWTaggedString(string: string).attributedString(with: options)
}

func tappableLabel(_ tappableLabel: ZSWTappableLabel, tappedAt idx: Int, withAttributes attributes: [NSAttributedStringKey : Any] = [:]) {
guard let url = attributes[.link] as? URL else {
return
}

UIApplication.shared.openURL(url)
}









share|improve this question















I'm using ZSWTappableLabel and ZSWTaggedString to show links inside a label.



import ZSWTappableLabel
import ZSWTaggedString


The pod versions are:



  pod 'ZSWTappableLabel', '~> 2.0'
pod 'ZSWTaggedString/Swift', '~> 4.0'


The links used to appear in white (same color as the label) by default earlier, but after some update that happened recently (possibly a pod update or xcode version, I'm not able to pinpoint exactly what), the links have started to appear in blue. Setting NSAttributedStringKey.foregroundColor to white doesn't seem to affect anything. NSAttributedStringKey.backgroundColor does affect it, but for some reason the foregroundColor doesn't seem to have any effect.



How can I set the links in white color?



func setTermsAndPrivacyLinkLabel(){
termsAndPrivacyLabel.tapDelegate = self

let options = ZSWTaggedStringOptions()
options["link"] = .dynamic({ tagName, tagAttributes, stringAttributes in
guard let type = tagAttributes["type"] as? String else {
return [NSAttributedStringKey : Any]()
}

var foundURL: NSURL?

switch type {
case "privacy":
foundURL = NSURL(string: "(privacyUrl)")!
case "tos":
foundURL = NSURL(string: "(termsUrl)")!
default:
break
}

guard let URL = foundURL else {
return [NSAttributedStringKey : Any]()
}

return [
.tappableRegion: true,
NSAttributedStringKey.foregroundColor: UIColor.white,
NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 13.0),
.link: foundURL
]
})

let string = NSLocalizedString("By logging in, you agree to our <link type='tos'>terms</link> and <link type='privacy'>privacy</link>.", comment: "")
termsAndPrivacyLabel.attributedText = try? ZSWTaggedString(string: string).attributedString(with: options)
}

func tappableLabel(_ tappableLabel: ZSWTappableLabel, tappedAt idx: Int, withAttributes attributes: [NSAttributedStringKey : Any] = [:]) {
guard let url = attributes[.link] as? URL else {
return
}

UIApplication.shared.openURL(url)
}






swift swift4 nsattributedstring nsattributedstringkey






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 5 at 19:13









Bob Gilmore

5,632104045




5,632104045










asked Oct 9 at 15:17









Prabhu

4,8432193155




4,8432193155












  • Upgrading to their latest version, and following their example code (it's a little different than this) helped to fix this issue.
    – Prabhu
    Nov 10 at 17:44


















  • Upgrading to their latest version, and following their example code (it's a little different than this) helped to fix this issue.
    – Prabhu
    Nov 10 at 17:44
















Upgrading to their latest version, and following their example code (it's a little different than this) helped to fix this issue.
– Prabhu
Nov 10 at 17:44




Upgrading to their latest version, and following their example code (it's a little different than this) helped to fix this issue.
– Prabhu
Nov 10 at 17:44












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted
+50










This works:



   extension HomeViewController:  ZSWTappableLabelTapDelegate {
static let urlAttributeName = NSAttributedStringKey(rawValue: "URL")

func setLinks(){
termsPrivacyLabel.tapDelegate = self

enum LinkType: String {
case privacy = "privacy"
case terms = "terms"

var URL: Foundation.URL {
switch self {
case .privacy:
return Foundation.URL(string: "myprivacyurl")!
case .terms:
return Foundation.URL(string: "mytermsurl")!
}
}
}

let options = ZSWTaggedStringOptions()
options["link"] = .dynamic({ tagName, tagAttributes, stringAttributes in
guard let typeString = tagAttributes["type"] as? String,
let type = LinkType(rawValue: typeString) else {
return [NSAttributedStringKey: AnyObject]()
}

return [
.tappableRegion: true,
.tappableHighlightedForegroundColor: UIColor.white,
.foregroundColor: UIColor.lightGray,
.underlineStyle: NSUnderlineStyle.styleNone.rawValue,
.font: UIFont.boldSystemFont(ofSize: 13.0),
HomeViewController.urlAttributeName: type.URL
]
})

let string = NSLocalizedString("By signing in, you agree to the <link type='terms'>terms</link> and <link type='privacy'>privacy</link>.", comment: "")
termsPrivacyLabel.attributedText = try? ZSWTaggedString(string: string).attributedString(with: options)
}

func tappableLabel(_ tappableLabel: ZSWTappableLabel, tappedAt idx: Int, withAttributes attributes: [NSAttributedStringKey : Any] = [:]) {
guard let URL = attributes[HomeViewController.urlAttributeName] as? URL else {
return
}

if #available(iOS 9, *) {
show(SFSafariViewController(url: URL), sender: self)
} else {
UIApplication.shared.openURL(URL)
}
}





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52724383%2fnsattributedstringkey-foregroundcolor-not-working%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted
    +50










    This works:



       extension HomeViewController:  ZSWTappableLabelTapDelegate {
    static let urlAttributeName = NSAttributedStringKey(rawValue: "URL")

    func setLinks(){
    termsPrivacyLabel.tapDelegate = self

    enum LinkType: String {
    case privacy = "privacy"
    case terms = "terms"

    var URL: Foundation.URL {
    switch self {
    case .privacy:
    return Foundation.URL(string: "myprivacyurl")!
    case .terms:
    return Foundation.URL(string: "mytermsurl")!
    }
    }
    }

    let options = ZSWTaggedStringOptions()
    options["link"] = .dynamic({ tagName, tagAttributes, stringAttributes in
    guard let typeString = tagAttributes["type"] as? String,
    let type = LinkType(rawValue: typeString) else {
    return [NSAttributedStringKey: AnyObject]()
    }

    return [
    .tappableRegion: true,
    .tappableHighlightedForegroundColor: UIColor.white,
    .foregroundColor: UIColor.lightGray,
    .underlineStyle: NSUnderlineStyle.styleNone.rawValue,
    .font: UIFont.boldSystemFont(ofSize: 13.0),
    HomeViewController.urlAttributeName: type.URL
    ]
    })

    let string = NSLocalizedString("By signing in, you agree to the <link type='terms'>terms</link> and <link type='privacy'>privacy</link>.", comment: "")
    termsPrivacyLabel.attributedText = try? ZSWTaggedString(string: string).attributedString(with: options)
    }

    func tappableLabel(_ tappableLabel: ZSWTappableLabel, tappedAt idx: Int, withAttributes attributes: [NSAttributedStringKey : Any] = [:]) {
    guard let URL = attributes[HomeViewController.urlAttributeName] as? URL else {
    return
    }

    if #available(iOS 9, *) {
    show(SFSafariViewController(url: URL), sender: self)
    } else {
    UIApplication.shared.openURL(URL)
    }
    }





    share|improve this answer

























      up vote
      1
      down vote



      accepted
      +50










      This works:



         extension HomeViewController:  ZSWTappableLabelTapDelegate {
      static let urlAttributeName = NSAttributedStringKey(rawValue: "URL")

      func setLinks(){
      termsPrivacyLabel.tapDelegate = self

      enum LinkType: String {
      case privacy = "privacy"
      case terms = "terms"

      var URL: Foundation.URL {
      switch self {
      case .privacy:
      return Foundation.URL(string: "myprivacyurl")!
      case .terms:
      return Foundation.URL(string: "mytermsurl")!
      }
      }
      }

      let options = ZSWTaggedStringOptions()
      options["link"] = .dynamic({ tagName, tagAttributes, stringAttributes in
      guard let typeString = tagAttributes["type"] as? String,
      let type = LinkType(rawValue: typeString) else {
      return [NSAttributedStringKey: AnyObject]()
      }

      return [
      .tappableRegion: true,
      .tappableHighlightedForegroundColor: UIColor.white,
      .foregroundColor: UIColor.lightGray,
      .underlineStyle: NSUnderlineStyle.styleNone.rawValue,
      .font: UIFont.boldSystemFont(ofSize: 13.0),
      HomeViewController.urlAttributeName: type.URL
      ]
      })

      let string = NSLocalizedString("By signing in, you agree to the <link type='terms'>terms</link> and <link type='privacy'>privacy</link>.", comment: "")
      termsPrivacyLabel.attributedText = try? ZSWTaggedString(string: string).attributedString(with: options)
      }

      func tappableLabel(_ tappableLabel: ZSWTappableLabel, tappedAt idx: Int, withAttributes attributes: [NSAttributedStringKey : Any] = [:]) {
      guard let URL = attributes[HomeViewController.urlAttributeName] as? URL else {
      return
      }

      if #available(iOS 9, *) {
      show(SFSafariViewController(url: URL), sender: self)
      } else {
      UIApplication.shared.openURL(URL)
      }
      }





      share|improve this answer























        up vote
        1
        down vote



        accepted
        +50







        up vote
        1
        down vote



        accepted
        +50




        +50




        This works:



           extension HomeViewController:  ZSWTappableLabelTapDelegate {
        static let urlAttributeName = NSAttributedStringKey(rawValue: "URL")

        func setLinks(){
        termsPrivacyLabel.tapDelegate = self

        enum LinkType: String {
        case privacy = "privacy"
        case terms = "terms"

        var URL: Foundation.URL {
        switch self {
        case .privacy:
        return Foundation.URL(string: "myprivacyurl")!
        case .terms:
        return Foundation.URL(string: "mytermsurl")!
        }
        }
        }

        let options = ZSWTaggedStringOptions()
        options["link"] = .dynamic({ tagName, tagAttributes, stringAttributes in
        guard let typeString = tagAttributes["type"] as? String,
        let type = LinkType(rawValue: typeString) else {
        return [NSAttributedStringKey: AnyObject]()
        }

        return [
        .tappableRegion: true,
        .tappableHighlightedForegroundColor: UIColor.white,
        .foregroundColor: UIColor.lightGray,
        .underlineStyle: NSUnderlineStyle.styleNone.rawValue,
        .font: UIFont.boldSystemFont(ofSize: 13.0),
        HomeViewController.urlAttributeName: type.URL
        ]
        })

        let string = NSLocalizedString("By signing in, you agree to the <link type='terms'>terms</link> and <link type='privacy'>privacy</link>.", comment: "")
        termsPrivacyLabel.attributedText = try? ZSWTaggedString(string: string).attributedString(with: options)
        }

        func tappableLabel(_ tappableLabel: ZSWTappableLabel, tappedAt idx: Int, withAttributes attributes: [NSAttributedStringKey : Any] = [:]) {
        guard let URL = attributes[HomeViewController.urlAttributeName] as? URL else {
        return
        }

        if #available(iOS 9, *) {
        show(SFSafariViewController(url: URL), sender: self)
        } else {
        UIApplication.shared.openURL(URL)
        }
        }





        share|improve this answer












        This works:



           extension HomeViewController:  ZSWTappableLabelTapDelegate {
        static let urlAttributeName = NSAttributedStringKey(rawValue: "URL")

        func setLinks(){
        termsPrivacyLabel.tapDelegate = self

        enum LinkType: String {
        case privacy = "privacy"
        case terms = "terms"

        var URL: Foundation.URL {
        switch self {
        case .privacy:
        return Foundation.URL(string: "myprivacyurl")!
        case .terms:
        return Foundation.URL(string: "mytermsurl")!
        }
        }
        }

        let options = ZSWTaggedStringOptions()
        options["link"] = .dynamic({ tagName, tagAttributes, stringAttributes in
        guard let typeString = tagAttributes["type"] as? String,
        let type = LinkType(rawValue: typeString) else {
        return [NSAttributedStringKey: AnyObject]()
        }

        return [
        .tappableRegion: true,
        .tappableHighlightedForegroundColor: UIColor.white,
        .foregroundColor: UIColor.lightGray,
        .underlineStyle: NSUnderlineStyle.styleNone.rawValue,
        .font: UIFont.boldSystemFont(ofSize: 13.0),
        HomeViewController.urlAttributeName: type.URL
        ]
        })

        let string = NSLocalizedString("By signing in, you agree to the <link type='terms'>terms</link> and <link type='privacy'>privacy</link>.", comment: "")
        termsPrivacyLabel.attributedText = try? ZSWTaggedString(string: string).attributedString(with: options)
        }

        func tappableLabel(_ tappableLabel: ZSWTappableLabel, tappedAt idx: Int, withAttributes attributes: [NSAttributedStringKey : Any] = [:]) {
        guard let URL = attributes[HomeViewController.urlAttributeName] as? URL else {
        return
        }

        if #available(iOS 9, *) {
        show(SFSafariViewController(url: URL), sender: self)
        } else {
        UIApplication.shared.openURL(URL)
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 17:48









        Prabhu

        4,8432193155




        4,8432193155






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52724383%2fnsattributedstringkey-foregroundcolor-not-working%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Error while running script in elastic search , gateway timeout

            Adding quotations to stringified JSON object values