【问题标题】:How to highlight strings if they are in UITableView in Swift3如果它们在 Swift3 的 UITableView 中,如何突出显示字符串
【发布时间】:2017-02-10 19:53:30
【问题描述】:

我想在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

  }

【问题讨论】:

  • 您知道比较不起作用还是没有着色?执行是在 if 还是 else 语句中?
  • 我的 allData 数组中有很多字符串,但其中一个只是突出显示
  • 你想使用第三部分库吗?
  • 是的,我可以使用 Cocoapods!

标签: swift uitableview for-loop


【解决方案1】:

首先,从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
}

并从 tableView 调用函数

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   ...

   cell.entryLabel.attributedText = entryTextColored(indexPath: indexPath)

    return cell
}

【讨论】:

  • 你可以改变你的高亮逻辑
【解决方案2】:

取自这里的答案: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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-17
    • 2012-12-08
    • 1970-01-01
    • 2019-09-13
    • 2015-04-28
    • 1970-01-01
    • 2021-12-28
    相关资源
    最近更新 更多