我正在使用ZSWTappableLabel
andZSWTaggedString
来显示标签内的链接。
import ZSWTappableLabel
import ZSWTaggedString
吊舱版本是:
pod 'ZSWTappableLabel', '~> 2.0'
pod 'ZSWTaggedString/Swift', '~> 4.0'
链接过去默认显示为白色(与标签相同的颜色),但在最近发生了一些更新(可能是 pod 更新或 xcode 版本,我无法准确指出是什么)之后,链接已经开始以蓝色出现。设置NSAttributedStringKey.foregroundColor
为白色似乎没有任何影响。NSAttributedStringKey.backgroundColor
确实会影响它,但由于某种原因,foregroundColor
它似乎没有任何影响。
如何将链接设置为白色?
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)
}