我想做一个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”
对于最后一行。
swift 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")
}
}
Out put is look like below screenshot
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)