Home Strikethrough UITableView cell text after cell containing String
Reply: 1

Strikethrough UITableView cell text after cell containing String

reinho14
1#
reinho14 Published in 2017-11-14 21:28:10Z

I'd like to strikethrough all cell text in UITableView after a specific line of text.

Example (after "+")

I think I'm close, but the strikethrough isn't applied in the Simulator. (build is successful)

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

    let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell")

   if indexPath.row > dailyTasks.index(of: "+")! {

    let attributeString: NSMutableAttributedString =  NSMutableAttributedString(string: dailyTasks[indexPath.row])
    attributeString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: 1, range: NSMakeRange(0, attributeString.length))

    cell.textLabel?.attributedText = attributeString

   } else {
     cell.textLabel?.text = dailyTasks[indexPath.row]
    }

    cell.textLabel?.font = UIFont(name: "GillSans-Light", size: 17)
    cell.indentationLevel = 1
    cell.textLabel?.addCharactersSpacing(1.2)
    cell.backgroundColor = UIColor(red:0.99, green:0.97, blue:0.91, alpha:1.0)
    cell.selectionStyle = UITableViewCellSelectionStyle.none

    return cell

}
Josh Homann
2#
Josh Homann Reply to 2017-11-14 21:53:24Z

Don't set the font on the label if you are using an attributed string; instead set the font as an attribute. Here is a playground that shows how to do it:

import UIKit
import PlaygroundSupport

let label = UILabel()
let text = "sample text"
let view = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
let attributes: [NSAttributedStringKey: Any] =
    [NSAttributedStringKey.font: UIFont(name: "GillSans-Light", size: 17)!,
     NSAttributedStringKey.foregroundColor : UIColor.white,
     NSAttributedStringKey.strikethroughStyle: 1]
label.attributedText = NSAttributedString(string: text, attributes: attributes)
view.addSubview(label)
label.sizeToFit()
PlaygroundPage.current.liveView = view
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.358338 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO