任何人都知道怎么做? 拥有自定义单元格后,子标签是一个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中有hss和css类(比方说ABC)。在属性字符串中是否有一种方式可以说是Foo类?或者除了从我从API获得的每个字符串使用正则表达式或字符串操作之外还有其他任何方式吗?它可以是数百个,它不是我的API所以我无法更改其中的HTML标记。
HTML文字示例:
bla bla bla <span class="HightlightTextClass"> highlighted text </span> more bla bra bra text
&#13;
&#34;
10倍
答案 0 :(得分:1)
您可以尝试使用def LetterChanges(str):
# code goes here
import string
ab_st = list(string.lowercase)
str = list(str)
new_word = []
for letter in range(len(str)):
if letter == "z":
new_word.append("a")
else:
new_word.append(ab_st[str.index(letter) + 1])
new_word = "".join(new_word)
return new_word
# keep this function call here
print LetterChanges(raw_input())
或类似内容插入自定义/usr/bin/python2.7 /home/vito/PycharmProjects/untitled1/test.py
test
Traceback (most recent call last):
File "/home/vito/PycharmProjects/untitled1/test.py", line 17, in <module>
print LetterChanges(raw_input())
File "/home/vito/PycharmProjects/untitled1/test.py", line 11, in LetterChanges
new_word.append(ab_st[str.index(letter) + 1])
ValueError: 0 is not in list
Process finished with exit code 1
代码,也可以通过实际使用属性字符串的文档属性来调整字体,颜色,背景颜色等。
答案 1 :(得分:0)
如果有任何一个感兴趣的人是这个问题的完整解决方案,那对我来说就像一个魅力:
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()
if let regex = try? NSRegularExpression(pattern: "class=\"hlt\"", options: .CaseInsensitive) {
let modStr = regex.stringByReplacingMatchesInString(searchRes.wordsClean, options: .WithTransparentBounds, range: NSMakeRange(0, searchRes.wordsClean.characters.count), withTemplate: "style=\"background-color:#F7DB6A\"")
let attrResult = try! NSAttributedString(
data: modStr.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!,
options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil)
cell.subtitleLabel.attributedText = attrResult
cell.subtitleLabel.textAlignment = NSTextAlignment.Right
}
}
return cell
}