import ZSWTappableLabelimport ZSWTaggedString
pod版本是:
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)}解决方法 这有效:
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,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,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) } }总结
以上是内存溢出为你收集整理的swift – NSAttributedStringKey.foregroundColor无效全部内容,希望文章能够帮你解决swift – NSAttributedStringKey.foregroundColor无效所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)