TTTAttributedLabel link 的 foregroundColor 显示不正确
TTTAttributedLabel link's foregroundColor didn't show correctly
我想匹配标签中所有的 MarkDown URL,所以我使用了 TTTAttributedLabel.It 的作品,除了 foregroundColor 没有正确显示。
这是我的全部代码:
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:[Whosebug](https://whosebug.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 不正确,我想知道是蓝色还是黑色。
你可以复制我的代码测试一下。
替换
tttLabel.text = "link1:[google](https://www.google.com),link2:[github](https://github.com),link3:[Whosebug](https://whosebug.com)"
来自
tttLabel.attributedText = NSAttributedString(string: "link1:[google](https://www.google.com),link2:[github](https://github.com),link3:[Whosebug](https://whosebug.com)")
最后我把NSAttributedString.Key.foregroundColor
改成kCTForegroundColorAttributeName
解决了这个问题。
我想匹配标签中所有的 MarkDown URL,所以我使用了 TTTAttributedLabel.It 的作品,除了 foregroundColor 没有正确显示。
这是我的全部代码:
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:[Whosebug](https://whosebug.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 不正确,我想知道是蓝色还是黑色。
你可以复制我的代码测试一下。
替换
tttLabel.text = "link1:[google](https://www.google.com),link2:[github](https://github.com),link3:[Whosebug](https://whosebug.com)"
来自
tttLabel.attributedText = NSAttributedString(string: "link1:[google](https://www.google.com),link2:[github](https://github.com),link3:[Whosebug](https://whosebug.com)")
最后我把NSAttributedString.Key.foregroundColor
改成kCTForegroundColorAttributeName
解决了这个问题。