2015-08-14 20 views
9

Ich möchte bestimmte Word anklickbare in UILabel Text mit Swift.Machen Sie anklickbare UILabel mit Swift

Ist es möglich?

Wenn mehr als ein Etikett vorhanden ist, wie kann ich erkennen, welches Wort gedrückt wird?

+1

hinzufügen tippen Geste zu beschriften Sie können dies mit seiner Aktion bekommen. –

Antwort

18

Sie können nicht mit dem einfachen Etikett tun.

Im Github ist eine Bibliothek verfügbar.

https://github.com/TTTAttributedLabel/TTTAttributedLabel

Daraus Sie die Methode yourLabel.addLinkToURL() genannt

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, Sir. Kannst du bitte als Antwort posten? Der Fragesteller wird also das richtige Ergebnis erhalten und auch antworten. –

+0

Ich versuche es. Ich habe das nicht getan :) Ich sehe es sofort –

+0

Ich glaube, die einfachste Lösung ist, 1-2 Etiketten und eine Taste zu verwenden. Auto-Layout macht es wirklich einfach. – 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

Ich möchte verwenden können, um teile meine Bibliothek https://github.com/psharanda/Atributika

Es enthält modernen Ersatz von TTTAtributedLabel + leistungsfähigem Satz von Methoden zur Erkennung und Stil unterschiedlicher Sachen wie Tags, Hashtags, erwähnen usw. (alles davon kann angeklickt werden)

Einige Codes zu zeigen, wie es funktioniert:

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