0

I want to highlight Strings in UITablViewCell, but this code does not help me to create that effect. The allData is CoreData and I loop it through to retrieve every element and if cell.textLabel.text has these elements, I want all elements from allData to be highlighted in color, in this example yellow. I What am I doing wrong here?

  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

  }
Ryo
  • 83
  • 11

2 Answers2

0

First of all, import BonMot library from github.

import BonMot

In this example if data contains abc it will implemented as 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
}

and call the function from tableView

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

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

    return cell
}
mathema
  • 939
  • 11
  • 22
0

Taken from this answer here: https://stackoverflow.com/a/35770826/2617369 I used the extension to NSMutableAttributedString to loop through the array of items and managed to get them all colored. You just have to use the cell.textLabel?.attributedText property of UILabel instead:

    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
}
Community
  • 1
  • 1
Prientus
  • 733
  • 6
  • 15