- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在使用 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, 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)
}
}
关于swift - NSAttributedStringKey.foregroundColor 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52724383/
我正在按照在线教程构建一个杂志类型的 iOS 应用程序。我正在尝试使用 NSAttributedStringKey 但不断收到如下所示的错误。有什么想法吗? 这是导致错误的代码行: let attrs
我想计算文本的高度以获得 Collection View 单元格的估计高度。我在 collectionViewLayout 函数中使用了以下代码; func collectionView(_ coll
可以通过 largeTitleTextAttributes 自定义新的大标题功能,(与任何其他属性一样) dictionary with NSAttributedStringKey 键。其中一个键是
处理一些 objC API,我收到一个 NSDictionary *>转换为 [String : Any]在 Swift 中,我将其用于 NSAttributedString.addAttribute
我正在使用 ZSWTappableLabel 和 ZSWTaggedString 来显示标签内的链接。 import ZSWTappableLabel import ZSWTaggedString p
这个问题在这里已经有了答案: How can I both stroke and fill with NSAttributedString w/ UILabel (5 个答案) 关闭 4 年前。 我
我最近转换了我的应用程序,但不断收到错误消息 "Cannot convert value of type '[String : Any]' to expected argument type '[NS
我在使用 NSAttributedStringKey.attachment 与 NSAttachmentAttributeName 时遇到了问题。相关代码如下: var key: Any?
我在使用库中的 Swift 代码时遇到了一些问题,我已经使用了一段时间。这似乎与某种版本冲突有关,但我不确定。 代码如下: let attribMsg = NSAttributedString(str
更新到 Swift 4 后,我收到此代码的错误 attributes["NSFont"] = font attributes["NSColor"] = UIColor(red: 0.0
下面是针对上述问题的代码行。此问题发生在 Swift 4.1 中。 let textAttributes = [NSAttributedStringKey.foregroundColor:UIColo
我正在尝试在我的应用程序中创建一个超链接,并且正在实现此解决方案: let attributedString = NSMutableAttributedString(string: "Just cli
刚刚更新了 xcode 9 的 pod,我收到以下关于 Cosmos 的错误. Type 'NSAttributedStringKey' (aka 'NSString') has no member
这个问题在这里已经有了答案: Swift 4 Cannot convert value of type '[String : AnyObject]?' to expected argument ty
我将我的代码从 Swift 3 转换为 Swift 4 但出现此错误: Type 'NSAttributedStringKey' has no member 'foregroundColorNSAtt
我对 NSAttributedStringKey 和 UITextView 有疑问 事实上,我正在尝试这样做: 这是理论上应该有效的代码 class ViewController: UIViewCon
将我的代码从 swift 3 转换为 4 并得到错误无法将类型“[String:Any]”的值转换为预期的参数类型“[NSAttributedStringKey:Any]” 在线 attributed
这个问题在这里已经有了答案: Cannot convert value of type '[String : AnyObject]?' to expected argument type '[NSA
这个问题在这里已经有了答案: Swift 4 attributedString get typing attributes (3 个答案) 关闭 5 年前。 我曾经能够生成 NSAttributed
iOS 部署目标:iOS 9.3,基础 SDK:最新的 iOS (iOS 11.0),Xcode 9 测试版 4, swift 4 以下代码在 Swift 3、Xcode 8 中构建和运行:
我是一名优秀的程序员,十分优秀!