ios - TTTAttributedLabel 链接的前景颜色未正确显示
问题描述
我想匹配标签中的所有 MarkDown URL,所以我使用了 TTTAttributedLabel。除了前景颜色没有正确显示外,它工作完美。
这是我的所有代码:
import UIKit
import TTTAttributedLabel
import SnapKit
class TTTLabelViewController2: UIViewController, TTTAttributedLabelDelegate {
let tttLabel = TTTAttributedLabel.init(frame: .zero)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
view.backgroundColor = .white
view.addSubview(tttLabel)
tttLabel.delegate = self
tttLabel.font = UIFont.systemFont(ofSize: 14, weight: .regular)
tttLabel.textColor = .black
tttLabel.isUserInteractionEnabled = true
tttLabel.numberOfLines = 10
tttLabel.backgroundColor = .orange
tttLabel.text = "link1:[google](https://www.google.com),link2:[github](https://github.com),link3:[stackoverflow](https://stackoverflow.com)"
tttLabel.snp.makeConstraints { (make) in
make.centerX.equalToSuperview()
make.top.equalTo(180)
make.width.equalTo(300)
make.height.equalTo(100)
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
checkMarkDownURL(label: tttLabel)
}
private func checkMarkDownURL(label: TTTAttributedLabel) {
let pattern = "(\\[.+?\\]\\([^\\)]+?\\))|(<.+?>)"
let regex = try? NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options.caseInsensitive)
var currentAttributedString: NSMutableAttributedString
if label.attributedText == nil {
currentAttributedString = NSMutableAttributedString.init(string: label.text as! String)
} else {
currentAttributedString = NSMutableAttributedString.init(attributedString: label.attributedText)
}
// match
if let result = regex?.firstMatch(in: currentAttributedString.string, options: [], range: NSRange.init(location: 0, length: currentAttributedString.string.count)) {
let url = (currentAttributedString.string as NSString).substring(with: result.range)
let linkName = url.components(separatedBy: ["[", "]"])[1]
let linkURL = url.components(separatedBy: ["(", ")"])[1]
let newRange = NSRange.init(location: result.range.location, length: linkName.count)
currentAttributedString.replaceCharacters(in: result.range, with: linkName)
// add link
label.addLink(with: NSTextCheckingResult.linkCheckingResult(range: newRange, url: URL.init(string: linkURL)!), attributes: [
NSAttributedString.Key.foregroundColor: UIColor.blue
])
// set color
currentAttributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.blue, range: newRange)
currentAttributedString.addAttribute(NSAttributedString.Key.backgroundColor, value: UIColor.gray, range: newRange)
currentAttributedString.addAttribute(NSAttributedString.Key.font, value: UIFont.boldSystemFont(ofSize: 14), range: newRange)
label.attributedText = currentAttributedString
// recursion
self.checkMarkDownURL(label: label)
}
}
// taped link
func attributedLabel(_ label: TTTAttributedLabel!, didSelectLinkWith url: URL!) {
print(url.absoluteString)
}
}
赛前:
比赛后:
如您所见,只有foregroundColor 不正确,我想知道它是蓝色但黑色。
您可以复制我的代码并进行测试。
解决方案
最后我通过更改NSAttributedString.Key.foregroundColor
为kCTForegroundColorAttributeName
.
推荐阅读
- python - 如何将自定义操作添加到图中并使其自动执行?
- java - AWS S3 - 带输入流的分段上传
- apache - 处理从 apache 到 NodeJS HTTP 服务器的连接
- javascript - 根据环境变量阻止 JEST 运行
- laravel-5.8 - 如何检索经过身份验证的用户的角色/状态?
- java - Java think 单选按钮始终处于选中状态
- java - Beam runner 如何确定 PCollection 的每个捆绑包的大小
- jquery - jQuery如何以可调整大小获取每个停止事件的值
- kubernetes - Grafana 为 Prometheus 数据源抛出 HTTP Error Bad Gateway
- firebase - 当我使用 Firebase Unity 插件在 android 中实现 FaceBook Auth 功能时,它崩溃了