swift 4 去除删除线
swift 4 remove strikethorugh
我点击的那一行会有删除线,点击一次后希望去掉删除线
有完成度默认值为false
let newList = List(name: name, completed: false)
我尝试删除删除线
cell.listName.attributedText = 无,
但它会随着我的文字消失
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)
}
试试这个:
let attributeString = NSMutableAttributedString(string: text)
attributeString.removeAttribute(NSAttributedStringKey.strikethroughStyle, range: NSMakeRange(0, attributeString.length))
attributeString.removeAttribute(NSAttributedStringKey.strikethroughColor, range: NSMakeRange(0, attributeString.length))
试试这个:
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: (self.yourLabel.text)!)
attributeString.removeAttribute(NSAttributedString.Key.strikethroughStyle, range: NSMakeRange(0, attributeString.length))
self.yourLabel.attributedText = attributeString
我点击的那一行会有删除线,点击一次后希望去掉删除线
有完成度默认值为false
let newList = List(name: name, completed: false)
我尝试删除删除线 cell.listName.attributedText = 无, 但它会随着我的文字消失
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)
}
试试这个:
let attributeString = NSMutableAttributedString(string: text)
attributeString.removeAttribute(NSAttributedStringKey.strikethroughStyle, range: NSMakeRange(0, attributeString.length))
attributeString.removeAttribute(NSAttributedStringKey.strikethroughColor, range: NSMakeRange(0, attributeString.length))
试试这个:
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: (self.yourLabel.text)!)
attributeString.removeAttribute(NSAttributedString.Key.strikethroughStyle, range: NSMakeRange(0, attributeString.length))
self.yourLabel.attributedText = attributeString