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)
}
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)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With