ios - 使用自定义 CSS 类从 HTML 中使用属性字符串填充单元格标签

标签 ios swift nsattributedstring

有人知道怎么做吗? 有一个自定义单元格,子标签是一个 HTML 片段,带有自定义 css 类,需要在那里为特定位置的文本着色。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = self.tableView!.dequeueReusableCellWithIdentifier("SearchResCell", forIndexPath: indexPath) as! SearchResCell

    if self.searchStore != nil && self.searchStore.searchResult.count > 0 && self.searchStore.searchResult[0].searchResults.count > 0 {
        let searchRes = searchStore.searchResult[0].searchResults[indexPath.row]

        cell.titleLabel.text = searchRes.displayTitle
        cell.titleLabel.textColor = UIColor.blueColor()

        let attrResult = try! NSAttributedString(
            data: searchRes.wordsClean.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!,
            options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
            documentAttributes: nil)
        cell.subtitleLabel.attributedText = attrResult
        cell.subtitleLabel.textAlignment = NSTextAlignment.Right

    }

    return cell
}

在 attrResult 中有带有 css 类的 html(比如说 ABC )。属性字符串中有没有办法说这是 Foo 类?或者除了对从 API 获取的每个字符串使用正则表达式或字符串操作之外,还有其他方法吗?它可能有数百个,而且它不是我的 API,所以我无法更改其中的 HTML 标签。

HTML 文本示例:

bla bla bla <span class="HightlightTextClass"> highlighted text </span> more bla bra bra text

10倍

最佳答案

您可以尝试使用 regex 或类似的方式插入自定义 style 标签,或者您可以通过实际使用文档来调整字体、颜色、背景颜色等属性字符串的属性。

关于ios - 使用自定义 CSS 类从 HTML 中使用属性字符串填充单元格标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37643653/

相关文章:

ios - Swift 3,框架 IBOutlet 属性始终展开可选

swift - 为什么我的 Playground 在 NSRange() 上崩溃

ios - 如何存储和访问 'array' 的 NSAttributedString.Key.foregroundColor 值

ios - 如何使用 React Native 重现 iOS 11 map Bottom Sheet

ios - iOS 建议的 GRG 非线性约束求解器

html - 如何在 webview 中设置 html 字符串 (swift 4.0)

ios - 如何在 swift 中格式化属性字符串?

objective-c - 保存一个自定义对象数组,每个对象包含一个自定义对象数组,最好的方法是什么?

ios - 如何设置 TouchableWithoutFeedback 的宽度取决于 react-native 中的文本?

iOS/Swift/FBSDKLoginKit : Login using FBSDKLoginManager(). logInWithReadPermissions 获取 result.isCancelled 为 true 失败