0

Ich muss einige tapGestures zu einem Teil eines Textes hinzufügen, das ist in einem UILabel. Es scheint in der Lage zu sein, einen Hyperlink zu erstellen - ich denke also, es ist auch möglich, einen Funktionsaufruf zu machen, aber ich bin mir nicht sicher, wie ich das machen soll.Wie Funktion-Aufruf (nicht Hyperlink) zu einem Teil eines NSAttributedString in einem UILabel hinzufügen?

Heres mein Code:

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 

Ich möchte die türkisfarbene Teile entweder auf Vertragsbedingungen für die Benutzer zu übernehmen zu können, oder Privat-Politik. Kann mir bitte jemand bei diesem Problem helfen? :))

Antwort

2

ich einmal die gleiche Sache gemacht, aber verwendet Textview statt UILabel, sollten Sie Ihre eigenen Attribute erstellen, und fügen Sie sie zurück String und Griff dann tippen Sie auf, hier einige Code

Code mit meinen Änderungen

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:)))) 

und func zu handhaben Hahn

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 
     } 
    } 
} 
+0

Super! Ich habe deine Lösung benutzt, mit ein bisschen Feinschliff - und jetzt funktioniert es wie ein Zauber! Danke vielmals –

0

Sie müssen einen normalen Hyperlink mit einem benutzerdefinierten Schema, z. myapp://function

und diese Methode in AppDelegate implementieren:

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 
    } 
} 
Verwandte Themen