如何在UILabel中添加函数调用(不是超链接)到NSAttributedString的一部分?

时间:2017-02-24 08:02:59

标签: ios swift uilabel nsattributedstring uitapgesturerecognizer

我需要在文本的某些部分添加一些tapGestures,即UILabel中的那些部分。它似乎能够创建一个超链接 - 所以我想它也可以进行功能调用,但我不知道该怎么做。

继承我的代码:

    let regularFont = UIFont(name: Constants.robotoRegular, size: 13)!
    let att1 = [NSFontAttributeName: regularFont]

    let turqoiseFont = UIFont(name: Constants.robotoBold, size: 13)!
    let att2 = [NSFontAttributeName: turqoiseFont]

    let attString1 = NSMutableAttributedString(string: "By creating a profile, I accept ", attributes: att1)
    attString1.addAttribute(NSForegroundColorAttributeName, value: UIColor.darkGrayColor(), range: NSMakeRange(0, attString1.length))

    let attString2 = NSMutableAttributedString(string: "Terms and Conditions ", attributes: att2)
    attString2.addAttribute(NSForegroundColorAttributeName, value: Colors.loginButtonColor, range: NSMakeRange(0, attString2.length))

    let attString3 = NSMutableAttributedString(string: "and ", attributes: att1)
    attString3.addAttribute(NSForegroundColorAttributeName, value: UIColor.darkGrayColor(), range: NSMakeRange(0, attString3.length))

    let attString4 = NSMutableAttributedString(string: "Private Policy.", attributes: att2)
    attString4.addAttribute(NSForegroundColorAttributeName, value: Colors.loginButtonColor, range: NSMakeRange(0, attString4.length))

    let index = "\(attString1.string.endIndex)"

    attString1.insertAttributedString(attString4, atIndex: Int(index)!)
    attString1.insertAttributedString(attString3, atIndex: Int(index)!)
    attString1.insertAttributedString(attString2, atIndex: Int(index)!)

    termsAndConditionLabel.attributedText = attString1

我希望turqoise部分能够将用户带到条款和条件,或私人政策。任何人都可以帮我解决这个问题吗? :))

3 个答案:

答案 0 :(得分:6)

我曾经做过同样的事情,但是使用textView而不是UILabel,你应该创建自己的属性,并将它们添加到属性字符串,然后处理tap,这里有一些代码

我的修改代码

   let regularFont = UIFont(name: "HelveticaNeue", size: 13)!
    let att1 = [NSFontAttributeName: regularFont]

    let turqoiseFont = UIFont(name: "HelveticaNeue", size: 13)!
    let att2 = [NSFontAttributeName: turqoiseFont]
    let mattString : NSMutableAttributedString  = NSMutableAttributedString()
    let attString1 = NSMutableAttributedString(string: "By creating a profile, I accept ", attributes: att1)
    attString1.addAttribute(NSForegroundColorAttributeName, value: UIColor.darkGray, range: NSMakeRange(0, attString1.length))
    let attString2 = NSMutableAttributedString(string: "Terms and Conditions ", attributes: att2)
    attString2.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: NSMakeRange(0, attString2.length))
    let attString3 = NSMutableAttributedString(string: "and ", attributes: att1)
    attString3.addAttribute(NSForegroundColorAttributeName, value: UIColor.darkGray, range: NSMakeRange(0, attString3.length))
    let attString4 = NSMutableAttributedString(string: "Private Policy.", attributes: att2)
    attString4.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: NSMakeRange(0, attString4.length))
    mattString.append(attString1)
    mattString.append(attString2)
    mattString.append(attString3)
    mattString.append(attString4)
    let attributes = ["link" : "termsLink"]
    let attributes2 = ["link" : "policyLink"]
    let str : NSString = mattString.string as NSString
    mattString.addAttributes(attributes, range: str.range(of: "Terms and Conditions"))
    mattString.addAttributes(attributes2, range: str.range(of: "Private Policy"))
    _textView.attributedText = mattString
    _textView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.singleTap(tap:)))) 

和func处理点击

func singleTap(tap : UITapGestureRecognizer) {
    let layoutManager = _textView.layoutManager
    let location = tap.location(in: _textView)
    let index = layoutManager.characterIndex(for: location, in: _textView.textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
    if index > _textView.textStorage.length {return}
    var range : NSRange = NSRange()
    if let type = _textView.attributedText.attribute("link", at: index, effectiveRange: &range) as? String {
        if type == "termsLink" {
            //.. do smth
        } else {
            //.. do smth
        }
    }
}

更新:

这是我写的使用Label

的类

https://github.com/barbados88/TapableLabel/blob/master/TapableLabel.swift

答案 1 :(得分:2)

我已通过 Swift 4 更改了Alexandr Kolesnik @{ int year = DateTime.Now.Year; } <select id="drpYillar"> <option value="1">@year+1 </option> <option value="2">@year </option> <option value="3">@year-1 </option> <option value="4">@year-2 </option> </select> 的回答。如果有人也想和UILabel一起使用

UILabel

和func来处理点击和检测点击的文本

    let regularFontAttribute = [NSAttributedStringKey.font: UIFont(name: "Lato-Regular", size: 15.0)!]

    let boldFontAttribute = [NSAttributedStringKey.font: UIFont(name: "Lato-Bold", size: 15.0)!]
    let mattString : NSMutableAttributedString  = NSMutableAttributedString()

    let attString1 = NSMutableAttributedString(string: "By creating a profile, I accept ", attributes: regularFontAttribute)
    let anotherAttribute1 = [NSAttributedStringKey.foregroundColor: UIColor.black]
    attString1.addAttributes(anotherAttribute1, range: NSMakeRange(0, attString1.length))

    let attString2 = NSMutableAttributedString(string: "Terms and Conditions ", attributes: boldFontAttribute)
    let anotherAttribute2 = [NSAttributedStringKey.foregroundColor: UIColor.red]
    attString2.addAttributes(anotherAttribute2, range: NSMakeRange(0, attString2.length))

    let attString3 = NSMutableAttributedString(string: "and ", attributes: regularFontAttribute)
    let anotherAttribute3 = [NSAttributedStringKey.foregroundColor: UIColor.black]
    attString3.addAttributes(anotherAttribute3, range: NSMakeRange(0, attString3.length))

    let attString4 = NSMutableAttributedString(string:  "Private Policy.", attributes: boldFontAttribute)
    let anotherAttribute4 = [NSAttributedStringKey.foregroundColor: UIColor.red]
    attString4.addAttributes(anotherAttribute4, range: NSMakeRange(0, attString4.length))

    mattString.append(attString1)
    mattString.append(attString2)
    mattString.append(attString3)
    mattString.append(attString4)

    let attributes: [NSAttributedStringKey : Any] = [NSAttributedStringKey(rawValue: "link") : "termsLink"]
    let attributes2: [NSAttributedStringKey : Any] = [NSAttributedStringKey(rawValue: "link") : "policyLink"]
    let str : NSString = mattString.string as NSString
    mattString.addAttributes(attributes, range: str.range(of: "Terms and Conditions"))
    mattString.addAttributes(attributes2, range: str.range(of: "Private Policy"))

    self.attributedLabel.attributedText = mattString
    self.attributedLabel.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.singleTap(_:))))
    self.attributedLabel.isUserInteractionEnabled = true

答案 2 :(得分:0)

您需要使用自定义方案执行常规超链接,例如myapp://function

并在您的app delegate中实现此方法:

optional func application(_ app: UIApplication,
                          open url: URL,
                          options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    guard let scheme = url.scheme, scheme == "myapp" else { return }
    if url.absoluteString.contains("function") {
         //run code
    }
}