I\'m trying to make separate pieces of text UILabel
s clickable. What I\'m looking for is commonly known as a hyperlink in web development.
The One approach would be something like the following.
The assumptions are:
UILabel
.UILabel
tag
has been set to the corresponding index in the arraylabelTapped:
is set as the touchUpInside
handler for the labels.import Foundation
import UIKit
class urltest {
var urls:[String]
init() {
self.urls=[String]() // Load URLs into here
}
@IBAction func labelTapped(sender:UILabel!) {
let urlIndex=sender.tag;
if (urlIndex >= 0 && urlIndex < self.urls.count) {
self.openUrl(self.urls[urlIndex]);
}
}
func openUrl(url:String!) {
let targetURL=NSURL.URLWithString(url)
let application=UIApplication.sharedApplication()
application.openURL(targetURL);
}
}