如果字符串位于 Swift3 中的 UITableView 中,如何突出显示字符串



我想在UITablViewCell中突出显示Strings,但这段代码不能帮助我创建这种效果。allDataCoreData的,我循环访问它以检索每个元素,如果cell.textLabel.text有这些元素,我希望allData中的所有元素都以颜色突出显示,在本例中为黄色。我在这里做错了什么?

  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let allData = Singlton.Singleton.getAllData()
    cell.textLabel?.text = someData[indexPath.row]
    cell.detailTextLabel?.text = ""
    for i in allData {
        if someData[indexPath.row].contains(i.string!.lowercased()) {
            cell.textLabel?.textColor = UIColor.yellow
        } else {
            cell.textLabel?.textColor = UIColor.white
        }
    }
       return cell
  }
  override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       return someData.count
  }

首先,从 github 导入 BonMot 库。

import BonMot

在此示例中,如果数据包含 abc,它将实现为 blueStyle

func entryTextColored(indexPath: IndexPath) -> NSAttributedString? {
    let string = entry?.entries?[indexPath.row].mobileEntryText
    let blueStyle = StringStyle(.color(UIColor(red:0.24, green:0.63, blue:0.68, alpha:1.00)))
    let mobileEntryStyle = StringStyle(
        .color(.darkGray),
        .xmlRules([
            .style("abc", blueStyle),
            ])
    )
    let attiributedString: NSAttributedString? = string?.styled(with: mobileEntryStyle)
    return attiributedString
}

并从表视图调用函数

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   ...
   cell.entryLabel.attributedText = entryTextColored(indexPath: indexPath)
    return cell
}

取自这个答案:https://stackoverflow.com/a/35770826/2617369我使用 NSMutableAttributedString 的扩展来遍历项目数组,并设法将它们全部着色。你只需要使用 UILabel 的 cell.textLabel?.attributedText 属性:

    extension NSMutableAttributedString {
        enum AtributeSearchType {
            case First, All, Last
        }
        func attributeRangeFor(searchString: String, attributeValue: AnyObject, atributeSearchType: AtributeSearchType) {
            let inputLength = self.string.characters.count
            let searchLength = searchString.characters.count
            var range = NSRange(location: 0, length: self.length)
            var rangeCollection = [NSRange]()
            while (range.location != NSNotFound) {
                range = (self.string as NSString).range(of: searchString, options: [], range: range)
                if (range.location != NSNotFound) {
                    switch atributeSearchType {
                    case .First:
                        self.addAttribute(NSForegroundColorAttributeName, value: attributeValue, range: NSRange(location: range.location, length: searchLength))
                        return
                    case .All:
                        self.addAttribute(NSForegroundColorAttributeName, value: attributeValue, range: NSRange(location: range.location, length: searchLength))
                        break
                    case .Last:
                        rangeCollection.append(range)
                        break
                    }
                    range = NSRange(location: range.location + range.length, length: inputLength - (range.location + range.length))
                }
            }
            switch atributeSearchType {
            case .Last:
                let indexOfLast = rangeCollection.count - 1
                self.addAttribute(NSForegroundColorAttributeName, value: attributeValue, range: rangeCollection[indexOfLast])
                break
            default:
                break
            }
        }
    }
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let allData = Singlton.Singleton.getAllData()
    cell.detailTextLabel?.text = ""
    var attributeText = NSMutableAttributedString(string: someData[indexPath.row])
    for i in allData {
        attributeText.attributeRangeFor(searchString: i, attributeValue: UIColor.yellow, atributeSearchType: .All)
    }
    cell.titleLabel?.attributedText = attributedText
    return cell
}

最新更新