【问题标题】:Multi-Line TableView Cell with Text Formatting具有文本格式的多行 TableView 单元格
【发布时间】:2018-12-25 07:30:02
【问题描述】:

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

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

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

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

请寻求反馈

【问题讨论】:

    标签: ios swift uitableview uilabel uitextview


    【解决方案1】:

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

    - 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-12
      • 2013-01-23
      • 2011-08-12
      • 1970-01-01
      • 1970-01-01
      • 2013-06-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多