ios - 具有文本格式的多行 TableView 单元格
问题描述
场景
我有一个 JSON 提要,我可以在其中获取有关城市中地点的所有这些信息。设计看起来像这样(见附图)。
设计需要粗体文本、URL 文本、不同字体大小的文本。有时,该 URL 可能不存在于 JSON 提要中。
问题
哪个选项是在 TableViewCell 中布局的最佳方式?
我认为选项 3 是最简单的,因为在 Swift 4 中您可以使用新的字符串文字。用换行符连接所有字符串,然后将它们放在 UITextView 中。但我认为它不支持格式化文本。
请寻求反馈
解决方案
更新:根据以下研究,我发现您可以使用以下方法解决此问题:
- NSAttributedStringKey
- NSAttributedString
- NSMutableAttributedString
第一步
我创建了一个名为的私有函数generateEntry
,它返回一个NSMutableAttributedString
private func generateEntry(bType: String, bTitle:String, bDescription:String, bUrl: String) -> NSMutableAttributedString {
// -- passed in parameters
let bookmarkType:String = bType
let bookmarkTitle:String = bTitle
let bookmarkDesc: String = bDescription
let bookmarkUrl: String = bUrl
// -- type
// Step 1 - you need to create the attribute for the string you want to change. Size, color, kern effect
// Step 2 - then you create a NSAttributedString and apply the attributes from the previous line
let bTypeAttribute = [NSAttributedStringKey.font: UIFont(name: "Arial", size: 14), NSAttributedStringKey.foregroundColor: UIColor.black, NSAttributedStringKey.kern: NSNumber(value: 0.5)]
let test: NSAttributedString = NSAttributedString(string: bookmarkType.uppercased() + ": ", attributes: bTypeAttribute as Any as? [NSAttributedStringKey : Any])
// -- title
let bTitleAttribute = [NSAttributedStringKey.font: UIFont(name: "Arial-Bold", size: 18), NSAttributedStringKey.foregroundColor: UIColor.black]
let testb: NSAttributedString = NSAttributedString(string: bookmarkTitle, attributes: bTitleAttribute as Any as? [NSAttributedStringKey : Any])
// -- description
let bDescriptionAttribute = [NSAttributedStringKey.font: UIFont(name: "Arial", size: 18), NSAttributedStringKey.foregroundColor: UIColor.black]
let testc: NSAttributedString = NSAttributedString(string: ": " + bookmarkDesc, attributes: bDescriptionAttribute as Any as? [NSAttributedStringKey : Any])
// -- url
let bURLAttribute = [NSAttributedStringKey.font: UIFont(name: "Arial-Italic", size: 18), NSAttributedStringKey.foregroundColor: UIColor.black]
let testd: NSAttributedString = NSAttributedString(string: bookmarkUrl, attributes: bURLAttribute as Any as? [NSAttributedStringKey : Any])
// combine the strings
let mutableAttributedString = NSMutableAttributedString()
mutableAttributedString.append(test)
mutableAttributedString.append(testb)
mutableAttributedString.append(testc)
mutableAttributedString.append(NSAttributedString(string: "\n"))
mutableAttributedString.append(testd)
return mutableAttributedString
}
第 2 步
然后在我的 tableViewCell 中,我可以使用这个私有方法来生成NSMutableAttributedString
并将其应用到我的自定义 tableViewCell。但不是使用cell.descriptionLabel.text
你需要确保使用attributedText
let testText = generateEntry(bType: "Type", bTitle: "PLACE", bDescription: "A description...", bUrl: "URL")
cell.descriptionLabel?.attributedText = testText
推荐阅读
- javascript - 如何将图像发送到jquery中的视图
- linux - 选择值时显示额外列的 Vala 组合框
- c++ - 我还可以添加什么来通过空格或空格正确拆分 C++ 中的字符串?
- excel - 如何自动更改颜色?
- ios - 如何在 iOS 的 Web Audio 实现中平移音频?
- pyspark - pyspark 将列的最大值保存到参数中?
- objective-c - Objective-C:点击 UIDownPicker 并获取 UITextfield 的值
- homebrew - 终端中显示的 commonmarker 错误消息
- overriding - 覆盖 sylius 的 customerController
- javascript - 使用 vuejs2 在 Chrome 上有一个待处理的进程,但在 Firefox 上没有