1

Issue

Setting

cell.textView.attributedText = NSAttributedString.strikeThroughText("Any sort of text Goes Here")

inside of a UITableViewCell causes the text itself to be replaced with a strikethrough the length of the actual text. The textView still holds the correct String value, but the text itself is not visible, only the strikethrough.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomCell

    cell.textView.attributedText = NSAttributedString.strikeThroughText(self.tasks[indexPath.row].name!)

    return cell
}

// Note - tableview is black, so the white text should be visible
extension NSAttributedString {
  static func strikeThroughText(_ text: String ) -> NSAttributedString {
    let attributeString: NSMutableAttributedString =  NSMutableAttributedString(string: text)
    attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length))
    attributeString.addAttribute(NSStrikethroughColorAttributeName, value: UIColor.white, range: NSMakeRange(0, attributeString.length))
    return attributeString
  }
}

enter image description here

enter image description here

Note

I am able to use this code correctly on a UILabel.

Question

How to correctly perform strikethrough on a UITextView's text

lifewithelliott
  • 1,012
  • 2
  • 16
  • 35

0 Answers0