2015-08-14 4 views
9

Voglio impostare Word particolare selezionabile nel testo UILabel utilizzando Swift.Rendi cliccabile UILabel utilizzando Swift

È possibile?

Se è presente più di un'etichetta, come posso rilevare quale parola viene premuta?

+1

aggiungi tocco gesto all'etichetta è possibile ottenere questo con la sua azione. –

risposta

18

Non si può fare con la semplice etichetta.

C'è una libreria disponibile nel github.

https://github.com/TTTAttributedLabel/TTTAttributedLabel

Da questo si può utilizzare il metodo chiamato yourLabel.addLinkToURL()

class ViewController: UIViewController , TTTAttributedLabelDelegate{ 

    @IBOutlet var lbl: TTTAttributedLabel! 
    override func viewDidLoad() { 
     super.viewDidLoad() 

     var str : NSString = "Hello this is link" 
     lbl.delegate = self 
     lbl.text = str as String 
     var range : NSRange = str.rangeOfString("link") 
     lbl.addLinkToURL(NSURL(string: "http://github.com/mattt/")!, withRange: range) 
    } 

    func attributedLabel(label: TTTAttributedLabel!, didSelectLinkWithURL url: NSURL!) { 
     UIApplication.sharedApplication().openURL(url) 
    } 
} 

enter image description here

+0

@ Daij-Djan Ok, signore. puoi postare come risposta? Quindi l'interrogante otterrà risultati corretti e anche la risposta. –

+0

Prova male. Non l'ho fatto :) Lo guardo subito –

+0

Credo che la soluzione più semplice sia usare 1-2 etichette e un pulsante. L'autolayout lo rende davvero facile. – Sulthan

1

SWIFT 3,0

privacyLabel.delegate = self 
    let strPolicy : NSString = "Agree to the Terms & Conditions" 
    privacyLabel.text = strPolicy as String 
    let range1 : NSRange = strPolicy.range(of: "Terms & Conditions") 
    privacyLabel.addLink(to: URL(string: "http://Terms.com")!, with: range1) 



    func attributedLabel(_ label: TTTAttributedLabel!, didSelectLinkWith url: URL!) { 

     print("url \(url)") 
      // UIApplication.sharedApplication().openURL(url) 
    } 
0

mi piacerebbe condividono la mia libreria https://github.com/psharanda/Atributika

Contiene sostituto moderno di TTTAtributedLabel + potente insieme di metodi per rilevare e cose diverse stile come i tag, hashtag, menzioni etc (tutto di che può essere cliccabile)

Alcuni codice per mostrare come works:

let link = Style 
     .font(.boldSystemFont(ofSize: 14)) 
     .foregroundColor(.black) 
     .foregroundColor(.red, .highlighted) 

    let tos = link.named("tos") 
    let pp = link.named("pp") 

    let all = Style 
     .font(.systemFont(ofSize: 14)) 
     .foregroundColor(.gray) 

    let text = "<tos>Terms of Service</tos> and <pp>Privacy Policy</pp>" 
     .style(tags: tos, pp) 
     .styleAll(all) 

    let tosLabel = AttributedLabel() 
    tosLabel.textAlignment = .center 
    tosLabel.attributedText = text 
    tosLabel.onClick = { label, detection in 
     switch detection.type { 
     case .tag(let tag): 
      switch tag.name { 
      case "pp": 
       print("Privacy Policy clicked") 
      case "tos": 
       print("Terms of Service clicked") 
      default: 
       break 
      } 
     default: 
      break 
     } 
    } 

    view.addSubview(tosLabel)