2017-10-05 19 views
3

J'utilise iOS 11 et Swift 4.Link UITextView ne fonctionne pas à Swift 4

Je suis en train de générer un lien dans programme un UITextView.

Tous les attributs fonctionnent (c'est-à-dire la couleur, la taille, la plage, etc.) - mais malheureusement, le lien ne fonctionne pas. Quelqu'un peut-il me dire pourquoi?

Voici mon code:

@IBOutlet weak var textView: UITextView! 

    // Setting the attributes 
    let linkAttributes = [ 
     NSAttributedStringKey.link: URL(string: "https://www.apple.com")!, 
     NSAttributedStringKey.font: UIFont(name: "Helvetica", size: 18.0)!, 
     NSAttributedStringKey.foregroundColor: UIColor.blue 
     ] as [NSAttributedStringKey : Any] 

    let attributedString = NSMutableAttributedString(string: "Just click here to do stuff...") 

    // Set the 'click here' substring to be the link 
    attributedString.setAttributes(linkAttributes, range: NSMakeRange(5, 10)) 

    self.textView.delegate = self 
    self.textView.attributedText = attributedString 

Encore une fois, le lien semble correct, mais en cliquant cela ne fonctionne pas.

Voici la capture d'écran:

Screenshot of the UITextView

Répondre

8

Vous devez activer et désactiver isSelectableisEditable pour votre mode texte.

textView.isSelectable = true 
textView.isEditable = false 

Vous pouvez également configurer ces comportements à l'intérieur Interface Builder dans le Comportement section du Attributs de vue du texte inspecteur:

Interface Builder - Attributes inspector/Behavior

+1

Merci! Je regardais trop loin. C'était intéressant de passer de Swift3 à Swift4 cette propriété «éditable» et «sélectionnable» d'une manière ou d'une autre à l'intérieur du story-board (puisque avant cela fonctionnait bien). Mieux vaut le faire par programme - merci! – iKK