【发布时间】:2016-11-09 17:19:31
【问题描述】:
我有一个 attributed string 设置为 UILabel 和 multiple underlines , colors 如下图所示
我知道如何为整个标签设置点击手势(启用user interaction)和下面是我的代码我所做的包括设置underline和设置@987654330 @ 代表multiple ranges。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var mylabel: UILabel!
var theString = "I have agree with the terms and conditions and privacy policy"
override func viewDidLoad() {
super.viewDidLoad()
mylabel.text = theString
let tap = UITapGestureRecognizer(target: self, action: #selector(ViewController.printme))
mylabel.addGestureRecognizer(tap)
setUnderline(theText: theString)
// Do any additional setup after loading the view, typically from a nib.
}
func printme() {
print("print this")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func setUnderline(theText : String) {
//set up underline
let textRange1 = NSMakeRange(22, 19)
let textRange2 = NSMakeRange(47, (theText.characters.count-47))
let attributedText = NSMutableAttributedString(string : theText)
attributedText.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.styleSingle.rawValue, range: textRange1)
attributedText.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.styleSingle.rawValue, range: textRange2)
//setup colors
attributedText.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: NSRange(location: 22,length: 20))
attributedText.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: NSRange(location: 47,length: (theText.characters.count-47)))
mylabel.attributedText = attributedText
}
-
tap手势适用于whole label。我想要的是当用户点击“条款和条件”时触发不同的功能,并且当用户点击“隐私政策”时触发另一个不同的功能。我怎样才能做到这一点。
注意:我想触发两个不同的功能,一个用于“条款和条件”点击,另一个用于“隐私政策”点击,并且不想只 打开链接
【问题讨论】:
标签: ios swift3 nsattributedstring uitapgesturerecognizer nsrange