我有一个可编程的UITableView。我想让其中一个字符串中的部分文本可单击。这有可能做到吗?
var moreSectionItems = [(image:String, title:String, subtitle:String)]()
moreSectionItems.append((image: "clouds", title: "Clouds", subtitle: "See the beautiful CLOUDS"))
moreSectionItems.append((image: "Stars", title: "Stars", subtitle: "See the beautiful STARS"))
sections.append((sectionTitle:"This is what you can see",sectionSubtitle: "See all the good things.",items:moreSectionItems))
特别是,我想让字幕中“云”和“星”的部分有下划线和可点击。有可能做到这一点吗?因为我需要一个属性字符串,但是tableview需要一个常规字符串。
发布于 2018-08-24 03:51:21
您可以使用label.attributtedText
和NSLinkAttributeName
class ViewController: UIViewController, UITextViewDelegate {
@IBOutlet var textView: UITextView!
override func viewDidLoad() {
let attributedString = NSMutableAttributedString(string: "Want to learn iOS? You should visit the best source of free iOS tutorials!")
attributedString.addAttribute(.link, value: "https://www.hackingwithswift.com", range: NSRange(location: 19, length: 55))
textView.attributedText = attributedString
}
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
UIApplication.shared.open(URL, options: [:])
return false
}
}
https://stackoverflow.com/questions/51991120
复制相似问题