【问题标题】:Is it possible to highlight a substring of text inside of a tableView cell?是否可以突出显示 tableView 单元格内的文本子字符串?
【发布时间】:2019-01-28 03:01:38
【问题描述】:

我有一个表格视图,可以使用搜索栏进行搜索。

我想将与传递到搜索栏的字符串匹配的文本部分加亮或加粗。

例如,如果 tableview 包含:“Apple”和“Orange”并且我键入字母“a”,我希望单元格现在包含“Apple”和“Or一个ge"

通过大量谷歌搜索,我还没有找到修改表格视图标题属性中文本子字符串的方法。

有人知道这是否可能吗?

这样的事情是我试图在表格视图中实现的:

【问题讨论】:

标签: ios swift uitableview


【解决方案1】:

请试试这个:

func attributedText(withString string: String, boldString: String, font: UIFont) -> NSAttributedString {
  let attributedString = NSMutableAttributedString(string: string,
                                             attributes: [NSAttributedString.Key.font: font])
  let boldFontAttribute: [NSAttributedString.Key: Any] = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: font.pointSize)]
  let range = (string as NSString).range(of: boldString)
  attributedString.addAttributes(boldFontAttribute, range: range)
  return attributedString
}

cellForRowAtIndexPath 方法中传递总字符串和搜索字符串,并将 return attributedString 分配给您的标签

它可能对您有所帮助。谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-28
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多