首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使部分编程UITableView文本可点击?

如何使部分编程UITableView文本可点击?
EN

Stack Overflow用户
提问于 2018-08-24 01:16:40
回答 1查看 123关注 0票数 0

我有一个可编程的UITableView。我想让其中一个字符串中的部分文本可单击。这有可能做到吗?

代码语言:javascript
复制
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需要一个常规字符串。

EN

回答 1

Stack Overflow用户

发布于 2018-08-24 03:51:21

您可以使用label.attributtedTextNSLinkAttributeName

https://www.hackingwithswift.com/example-code/system/how-to-make-tappable-links-in-nsattributedstring

代码语言:javascript
复制
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
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51991120

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档