首页 > 解决方案 > 具有文本格式的多行 TableView 单元格

问题描述

在此处输入图像描述

场景
我有一个 JSON 提要,我可以在其中获取有关城市中地点的所有这些信息。设计看起来像这样(见附图)。

设计需要粗体文本URL 文本不同字体大小的文本。有时,该 URL 可能不存在于 JSON 提要中。

问题
哪个选项是在 TableViewCell 中布局的最佳方式?

我认为选项 3 是最简单的,因为在 Swift 4 中您可以使用新的字符串文字。用换行符连接所有字符串,然后将它们放在 UITextView 中。但我认为它不支持格式化文本。

请寻求反馈

标签: iosswiftuitableviewuilabeluitextview

解决方案


更新:根据以下研究,我发现您可以使用以下方法解决此问题:

- 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

推荐阅读