强调uiLabel的一部分并链接带下划线的部分

Pin*_*ndo 7 click uilabel ios swift

我是iOS开发的新手.

我有一个标签,LatestInfo这里有文字,并且有一个链接到网站:例如For the latest information visit example.com/latestInfo

我希望显示为URL加下划线example.com/latestInfo并使其可点击.

我使用的是Swift而不是Obejective-C

我该怎么做呢?

根据皮埃尔的要求编辑:

@IBOutlet weak var linkLabel: UITextView!
let string              = "A great link : Google"
let range               = (string as NSString).rangeOfString("Google")
let attributedString    = NSMutableAttributedString(string: string)

attributedString.addAttribute(NSLinkAttributeName, value: NSURL("http://www.google.fr")!, range: range)
attributedString.addAttribute(NSUnderlineStyleAttributeName, value: NSNumber(int: 1), range: range)
attributedString.addAttribute(NSUnderlineColorAttributeName, value: UIColor.orangeColor(), range: range)

linkLabel.attributedText = attributedString 
Run Code Online (Sandbox Code Playgroud)

Pie*_*rre 14

查找NSMutableAttributedString,尤其是NSLinkAttributeName.有很多关于这方面的教程和Stackoverflow问题.您还可以阅读Apple关于属性字符串的文档TextView是唯一能够打开链接的组件.所以只需用你的标签替换你的标签:

let string              = "A great link : Google"
let range               = (string as NSString).rangeOfString("Google")
let attributedString    = NSMutableAttributedString(string: string)

attributedString.addAttribute(NSLinkAttributeName, value: NSURL("http://www.google.fr")!, range: range)
attributedString.addAttribute(NSUnderlineStyleAttributeName, value: NSNumber(int: 1), range: range)
attributedString.addAttribute(NSUnderlineColorAttributeName, value: UIColor.orangeColor(), range: range)


textView.attributedText = attributedString
Run Code Online (Sandbox Code Playgroud)


San*_*tel 5

在此输入图像描述

Please create one UILabel & check it's properties. 
Please select Text on first changed it's to plain to Attributed.

    Now you can seen you label text in one Textfield. select that text & right click to you mouse & goto Font menu. you can seen Underline. select it. you can seen underline in your Label.
Run Code Online (Sandbox Code Playgroud)