我已使用此String
扩展名将strikeThrough添加到文本
这是扩展名:
extension String{
func strikeThrough()->NSAttributedString{
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: self)
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: NSMakeRange(0, attributeString.length))
return attributeString
}
这里是UILabel
的用法:
if isDone {
self.title?.attributedText = title.strikeThrough()
} else {
self.title?.attributedText = nil
self.title?.text = title
}
它在iOS 12中运行良好,但是在iOS 13中,self.title?.attributedText = nil
不会删除strikeThrough行。
在我的应用程序中,将strikeThrough添加到文本上时,它将从列表中删除,但是现在,它已被删除,但是strikeThrough将出现在其他文本上,这是不应该发生的。
还有self.title?也不是零。
您能建议我从文本中删除strikeThrough
的任何方法,带有nil
的当前表格不起作用。
非常感谢您
答案 0 :(得分:0)
您可以在控制器中定义strikeThrough
,而不是使用扩展名。
例如:
let strokeEffect: [NSAttributedString.Key : Any] = [
NSAttributedString.Key.strikethroughStyle: NSUnderlineStyle.single.rawValue,
NSAttributedString.Key.strikethroughColor: UIColor.gray]
self.title?.attributedText = NSAttributedString(string: title, attributes: strokeEffect)
然后要删除strikeThrough
,只需更改其颜色即可清除...
答案 1 :(得分:0)
我在iOS13上遇到了同样的问题,我在tableviewCell中使用attrbutedText是这样的:
if(model.finished){
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString: @"finished_titleString..." ];
[attributeString addAttribute:NSStrikethroughStyleAttributeName
value:[NSNumber numberWithInt:NSUnderlineStyleSingle]
range:NSMakeRange(0, [attributeString length])];
cell.leftTitleLabel.attributedText = attributeString;
}else {
NSMutableAttributedString *mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"notfinished_titleString..."];
cell.leftTitleLbl.attributedText = mutableAttriStr;
}
在iOS13版本下都可以。
但是现在在iOS13上,如果我选中“完成列表”,则该单元格将显示为StrikethroughStyle,但随后我将其更改为“未完成列表”,该单元格也将显示为StrikethroughStyle。
为此,我尝试为单元格创建两种标识符,并且它可以正常工作。
UITableviewCell *cell = nil;
if(model.finished){
cell = [tableView dequeueReusableCellWithIdentifier:@"finishedCell"];
}else {
cell = [tableView dequeueReusableCellWithIdentifier:@"notFinishedCell"];
}
if (cell == nil) {
cell = [[ETMTodoFlowCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: model.finished ? @"finishedCell": @"notFinishedCell" ];
}
...
答案 2 :(得分:0)
在不想让它再次显示的情况下,我通过将value设置为0使它起作用。
Flutter
答案 3 :(得分:0)
我将此添加到了我的字符串扩展名,并且可以正常使用。请记住,您正在使用黑暗模式:
func strikeThrough() -> NSAttributedString {
let attributeString = NSMutableAttributedString(string: self)
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle,
value: NSUnderlineStyle.single.rawValue,
range: NSMakeRange(0, attributeString.length))
attributeString.addAttribute(NSAttributedString.Key.strikethroughColor,
value: UIColor.black,
range: NSMakeRange(0, attributeString.length))
return attributeString
}
func noStrikeThrough() -> NSAttributedString {
let attributeString = NSMutableAttributedString(string: self)
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle,
value: NSUnderlineStyle.single.rawValue,
range: NSMakeRange(0, attributeString.length))
attributeString.addAttribute(NSAttributedString.Key.strikethroughColor,
value: UIColor.clear,
range: NSMakeRange(0, attributeString.length))
return attributeString
}