有人知道怎么做吗? 有一个自定义单元格,子标签是一个 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/