带有 Swift 的 TTTAttributedLabel 中的可点击链接
Posted
技术标签:
【中文标题】带有 Swift 的 TTTAttributedLabel 中的可点击链接【英文标题】:Clickable link in TTTAttributedLabel with Swift 【发布时间】:2014-12-09 15:21:29 【问题描述】:我想制作一个UILabel
,其中包含一些带有可点击链接的文本。不是指向网页的链接,而是指向我对UIButton
所做的操作。所以我使用了TTTAttributedLabel
,它与Objective C
完美配合。现在我想在Swift
做同样的事情,所以我写了下面的代码:
self.someLabel.text = NSLocalizedString("Lost? Learn more.", comment: "")
let range = self.someLabel.text!.rangeOfString(NSLocalizedString("Learn more", comment:""))
self.someLabel.addLinkToURL (NSURL(string:"action://Learn more"), withRange:NSRange (range))
但是,我无法使链接在Swift
中工作。我收到错误消息:“Missing argument for parameter 'host' in call”
最后一行。
【问题讨论】:
试试NSURL(string:"action://learn-more")
似乎问题出在 NSRange 上。迅速它只接受 Rangeswift 4.2 中的 TTTAttributedLabel 标签
import TTTAttributedLabel
@IBOutlet weak var attributedLable: TTTAttributedLabel!
override func viewDidLoad()
super.viewDidLoad()
self.setup()
func setup()
attributedLable.numberOfLines = 0;
let strTC = "terms and conditions"
let strPP = "privacy policy"
let string = "By signing up or logging in, you agree to our \(strTC) and \(strPP)"
let nsString = string as NSString
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineHeightMultiple = 1.2
let fullAttributedString = NSAttributedString(string:string, attributes: [
NSAttributedString.Key.paragraphStyle: paragraphStyle,
NSAttributedString.Key.foregroundColor: UIColor.black.cgColor,
])
attributedLable.textAlignment = .center
attributedLable.attributedText = fullAttributedString;
let rangeTC = nsString.range(of: strTC)
let rangePP = nsString.range(of: strPP)
let ppLinkAttributes: [String: Any] = [
NSAttributedString.Key.foregroundColor.rawValue: UIColor.blue.cgColor,
NSAttributedString.Key.underlineStyle.rawValue: false,
]
let ppActiveLinkAttributes: [String: Any] = [
NSAttributedString.Key.foregroundColor.rawValue: UIColor.blue.cgColor,
NSAttributedString.Key.underlineStyle.rawValue: false,
]
attributedLable.activeLinkAttributes = ppActiveLinkAttributes
attributedLable.linkAttributes = ppLinkAttributes
let urlTC = URL(string: "action://TC")!
let urlPP = URL(string: "action://PP")!
attributedLable.addLink(to: urlTC, with: rangeTC)
attributedLable.addLink(to: urlPP, with: rangePP)
attributedLable.textColor = UIColor.black;
attributedLable.delegate = self;
func attributedLabel(_ label: TTTAttributedLabel!, didSelectLinkWith url: URL!)
if url.absoluteString == "action://TC"
print("TC click")
else if url.absoluteString == "action://PP"
print("PP click")
输出如下图所示
【讨论】:
嗨@Hardik 我想要不同颜色的条款和条件以及隐私政策。你能帮帮我吗? 你需要添加委托方法和attributedLable.delegate = self【参考方案2】:String.rangeOfString
返回Range
,但NSString.rangeOfString
返回NSRange
。所以下面的代码应该可以工作:
let name = "tomo"
let string = "My name is \(name)"
label.text = string
let nsString = string as NSString
let range = nsString.rangeOfString(name)
let url = NSURL(string: "action://users/\(name)")!
label.addLinkToURL(url, withRange: range)
【讨论】:
如果你能解释这是做什么的,而不是留下代码,那将是首选:) 我已添加说明! @埃米尔以上是关于带有 Swift 的 TTTAttributedLabel 中的可点击链接的主要内容,如果未能解决你的问题,请参考以下文章
swift 带有Swift 4 ios应用程序填充的圆形视图