Ios 如何更改UILabel中链接的颜色?
我想更改UILabel中链接的颜色。我发现了大量关于如何为UITextView执行此操作的过去问题,以及在Obj-C中包含答案的过去问题(但无法将这些问题转换为Swift,因为Obj-C中确实存在的属性已不再存在,例如NSMutableAttributeString.linkTextAttribtues)。Ios 如何更改UILabel中链接的颜色?,ios,swift,Ios,Swift,我想更改UILabel中链接的颜色。我发现了大量关于如何为UITextView执行此操作的过去问题,以及在Obj-C中包含答案的过去问题(但无法将这些问题转换为Swift,因为Obj-C中确实存在的属性已不再存在,例如NSMutableAttributeString.linkTextAttribtues)。 但是我找不到如何为UILabel和Swift 4做到这一点。使用UITextView而不是UILabel更容易,并且可以编写如下内容: textView.linkTextAttributes
但是我找不到如何为UILabel和Swift 4做到这一点。使用UITextView而不是UILabel更容易,并且可以编写如下内容:
textView.linkTextAttributes = [
.foregroundColor: UIColor.red,
.underlineColor: UIColor.red,
.underlineStyle: NSUnderlineStyle.single.rawValue
]
对于默认NSAttributedString.Key.链接颜色将为蓝色。如果需要链接的自定义颜色,可以将属性设置为NSAttributedString.Key。附件而不是。链接并按如下方式设置前景和下划线颜色:
let linkCustomAttributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14),
NSAttributedString.Key.foregroundColor: UIColor.red,
NSAttributedString.Key.underlineColor: UIColor.magenta,
NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue,
NSAttributedString.Key.attachment: URL(string: "https://www.google.com")] as [NSAttributedString.Key : Any]
如果需要处理链接上的触摸,可以使用此自定义标签类:
import UIKit
public protocol UILabelTapableLinksDelegate: NSObjectProtocol {
func tapableLabel(_ label: UILabelTapableLinks, didTapUrl url: String, atRange range: NSRange)
}
public class UILabelTapableLinks: UILabel {
private var links: [String: NSRange] = [:]
private(set) var layoutManager = NSLayoutManager()
private(set) var textContainer = NSTextContainer(size: CGSize.zero)
private(set) var textStorage = NSTextStorage() {
didSet {
textStorage.addLayoutManager(layoutManager)
}
}
public weak var delegate: UILabelTapableLinksDelegate?
public override var attributedText: NSAttributedString? {
didSet {
if let attributedText = attributedText {
textStorage = NSTextStorage(attributedString: attributedText)
findLinksAndRange(attributeString: attributedText)
} else {
textStorage = NSTextStorage()
links = [:]
}
}
}
public override var lineBreakMode: NSLineBreakMode {
didSet {
textContainer.lineBreakMode = lineBreakMode
}
}
public override var numberOfLines: Int {
didSet {
textContainer.maximumNumberOfLines = numberOfLines
}
}
public override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
private func setup() {
isUserInteractionEnabled = true
layoutManager.addTextContainer(textContainer)
textContainer.lineFragmentPadding = 0
textContainer.lineBreakMode = lineBreakMode
textContainer.maximumNumberOfLines = numberOfLines
}
public override func layoutSubviews() {
super.layoutSubviews()
textContainer.size = bounds.size
}
private func findLinksAndRange(attributeString: NSAttributedString) {
links = [:]
let enumerationBlock: (Any?, NSRange, UnsafeMutablePointer<ObjCBool>) -> Void = { [weak self] value, range, isStop in
guard let strongSelf = self else { return }
if let value = value {
let stringValue = "\(value)"
strongSelf.links[stringValue] = range
}
}
attributeString.enumerateAttribute(.link, in: NSRange(0..<attributeString.length), options: [.longestEffectiveRangeNotRequired], using: enumerationBlock)
attributeString.enumerateAttribute(.attachment, in: NSRange(0..<attributeString.length), options: [.longestEffectiveRangeNotRequired], using: enumerationBlock)
}
public override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let locationOfTouch = touches.first?.location(in: self) else {
return
}
textContainer.size = bounds.size
let indexOfCharacter = layoutManager.glyphIndex(for: locationOfTouch, in: textContainer)
for (urlString, range) in links where NSLocationInRange(indexOfCharacter, range) {
delegate?.tapableLabel(self, didTapUrl: urlString, atRange: range)
return
}
}
}
上面的答案是正确的,但是设置
.attachment
,因为url不会打开url,至少对我来说不会(使用iOS 13)。
.link
的颜色不受nsattributed string
中的.foregroundColor
的影响,而是受UITextView的tintColor
的影响
let urlAttributes: [NSAttributedString.Key: Any] = [
.link: URL(string:"https://google.com"),
.foregroundColor: textColor,
.underlineColor: textColor,
.underlineStyle: NSUnderlineStyle.single.rawValue
.underlinColor: UIColor.green
]
textView.tintColor = UIColor.green
textView.attributed = urlAttributes
应将链接的文本和下划线设置为绿色尝试此功能,它位于Objective-C桥接器中,并启用链接检测UIKIT功能在Swift中与在ObjC中相同;只是名称改变了。谢谢,但是那里有太多的代码,仅仅设置链接颜色就太过分了。用大锤敲打花生。在Swift中,必须有一行或两行的方式来直接执行该操作。@Josh Caswell,查看UILabel和可变属性字符串的声明,我看不到任何名称与linkTextAttributes或这两个名称中的任何一个名称非常相似的内容。那么在Swift 4中linkTextAttributes被重命名为什么呢?不确定;我相信这是在UITextView
上,而不是NSAttributedString
,但我只是想说,如果它在ObjC中,它也可以在Swift中使用。这不起作用。如果标签居中,即使单击填充链接占用的空白空间,它也会报告glyph索引,标签和属性字符串只是后面的一个难题。这门课刚刚开始。非常有用。谢谢
extension YourClass: UILabelTapableLinksDelegate {
func tapableLabel(_ label: UILabelTapableLinks, didTapUrl url: String, atRange range: NSRange) {
print("didTapUrl: ", url)
}
}
let urlAttributes: [NSAttributedString.Key: Any] = [
.link: URL(string:"https://google.com"),
.foregroundColor: textColor,
.underlineColor: textColor,
.underlineStyle: NSUnderlineStyle.single.rawValue
.underlinColor: UIColor.green
]
textView.tintColor = UIColor.green
textView.attributed = urlAttributes