ios - 如何使编程 UITableView 文本的一部分可点击?
问题描述
我有一个程序化的 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 需要一个常规字符串。
解决方案
您可以使用 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
}
}
推荐阅读
- swift - AF 没有成员“responseDecodable”与路由器
- mpandroidchart - 尝试在空对象引用 BarChart 上调用虚拟方法 'com.github.mikephil.charting.charts.BarChart.setMarker'
- c# - 使用 Cudafy 获取总 GPU 内存。返回最大 ulong 值
- python - 如何在 Python 中循环并增加 Excel 工作簿公式中的行?
- text - 允许从选定文件创建项目的 Macos 文本编辑器
- javascript - 如何从外部访问 js 包文件中的变量?
- python - 在 jupyter notebook 上显示 selenium webdriver 打开的 url
- python - 如何在python中模拟后续函数调用?
- python - SGD算法从零开始预测电影评分
- python - 如何使用 cv2 计算图像中的不同颗粒?