2015-07-16 31 views
5

Tôi mới phát triển iOS.phần gạch chân của phần uiLabel và liên kết được gạch chân

Tôi có nhãn LatestInfo văn bản này có văn bản và có nghĩa là có liên kết đến một trang web: ví dụ: For the latest information visit example.com/latestInfo

Tôi muốn màn hình hiển thị để gạch chân url example.com/latestInfo và làm cho nó có thể nhấp.

Tôi đang sử dụng Swift không Obejective-C

Tôi làm cách nào để thực hiện việc này?

EDIT theo yêu cầu của Pierre:

@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 
+0

có một cái nhìn tại này [SO] (http://stackoverflow.com/questions/28053334/how-to-underline- a-uilabel-in-swift) liên kết – Akhilrajtr

Trả lời

12

Hãy tìm NSMutableAttributedString và đặc biệt là cho NSLinkAttributeName. Có rất nhiều hướng dẫn và câu hỏi Stackoverflow về điều đó. Bạn cũng có thể đọc tài liệu của Apple về chuỗi được phân bổ TextView là thành phần duy nhất có thể mở liên kết. Vì vậy, chỉ cần thay thế nhãn của bạn với điều đó và:

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 
+0

Tôi đã quản lý gạch chân văn bản. Làm thế nào tôi có thể làm cho phần văn bản này có thể nhấp được? – Pindo

+0

Tôi đã chỉnh sửa câu trả lời của mình. Đó là những gì bạn muốn? – Pierre

+0

Điều đó làm cho nó trở thành một liên kết. Tôi muốn liên kết này được mở trong safari – Pindo

1

enter image description here

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. 
Các vấn đề liên quan