NSAttributedStringKey.foregroundColor not working
up vote
5
down vote
favorite
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
add a comment |
up vote
5
down vote
favorite
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
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
add a comment |
up vote
5
down vote
favorite
up vote
5
down vote
favorite
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
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
swift swift4 nsattributedstring nsattributedstringkey
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
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)
}
}
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
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)
}
}
add a comment |
up vote
1
down vote
accepted
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)
}
}
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
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)
}
}
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)
}
}
answered Nov 10 at 17:48
Prabhu
4,8432193155
4,8432193155
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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