加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift – NSAttributedStringKey.foregroundColor无效

发布时间:2020-12-14 04:56:41 所属栏目:百科 来源:网络整理
导读:我正在使用ZSWTappableLabel和ZSWTaggedString来显示标签内的链接. import ZSWTappableLabelimport ZSWTaggedString pod版本是: pod 'ZSWTappableLabel','~ 2.0' pod 'ZSWTaggedString/Swift','~ 4.0' 默认情况下,链接过去常常以白色显示(与标签颜色相同),
我正在使用ZSWTappableLabel和ZSWTaggedString来显示标签内的链接.

import ZSWTappableLabel
import 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)
            }
        }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读