swift 4 удалить зачеркнутый - PullRequest
0 голосов
/ 11 июня 2018

Я нажимаю на строку, где будет зачеркивание, после щелчка один раз надеюсь удалить зачеркивание

Существует завершенное значение степени по умолчанию: false

let newList = List (имя: имя, завершено: false)

Я попытался удалить зачеркивание с помощью cell.listName.attributedText = nil, но оно исчезнет с моим текстом

func completeList(_ indexPath: IndexPath) {
    let lists = self.lists[indexPath.row]
    self.lists[indexPath.row] = lists
    if let cell = listTableView.cellForRow(at: indexPath) as? ListTableViewCell {
        cell.listName.attributedText = strikeThroughText(lists.name!)
    }
}

// 刪除線
func strikeThroughText(_ text: String) -> NSAttributedString {
    let attributeString = NSMutableAttributedString(string: text)
    attributeString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: 2, range: NSMakeRange(0, attributeString.length))
    attributeString.addAttribute(NSAttributedStringKey.strikethroughColor, value:UIColor(red: 235, green: 86, blue: 87), range: NSMakeRange(0, attributeString.length))

    return attributeString
}

TableViewДелегат, источник данных

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return lists.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let listCell = tableView.dequeueReusableCell(withIdentifier: "listCell") as! ListTableViewCell

    let listItem = lists[indexPath.row]
    listCell.listName.text = listItem.name

    if listItem.completed {
        listCell.listName.attributedText = strikeThroughText(listItem.name!)
    }

    listCell.backgroundColor = UIColor.clear
    listCell.selectionStyle = .none
    return listCell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    completeList(indexPath)

}

1 Ответ

0 голосов
/ 11 июня 2018

Попробуйте это:

let attributeString = NSMutableAttributedString(string: text)
attributeString.removeAttribute(NSAttributedStringKey.strikethroughStyle, range: NSMakeRange(0, attributeString.length))
attributeString.removeAttribute(NSAttributedStringKey.strikethroughColor, range: NSMakeRange(0, attributeString.length))
...