我使用了这个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当前的表单不起作用。
非常感谢
发布于 2019-09-28 02:51:44
您可以在控制器中定义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,只需将其颜色更改为清除.
发布于 2019-11-05 17:49:43
我让它在设置值为0的情况下工作,如果我不想再显示它。
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: weightString)
//HACK we have to set strikethrough to 0 for ios 13 or it won't remove the strike even if setting the attributed text to nil
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 0, range: NSMakeRange(0, attributeString.length))
cell.weightLabel.attributedText = attributeString))发布于 2019-11-04 11:21:46
我在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" ];
}
...解决这个问题的另一种更优雅的方法是:
- (NSMutableAttributedString *)getNoStrikethroughAttribute:(NSString *)str andTextColor:(UIColor *)textColor{
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:str ];
[attributeString addAttribute:NSStrikethroughStyleAttributeName
value:[NSNumber numberWithInt:0]
range:NSMakeRange(0, [attributeString length])];
if (textColor) {
[attributeString addAttribute:NSForegroundColorAttributeName
value:textColor
range:NSMakeRange(0, [attributeString length])];
}
return attributeString;
}
if (model.finished) {
NSMutableAttributedString *mutableAttriStr = [Self getStrikethroughAttribute:text andTextColor:UIColorFromRGB(0x999999)];
cell.leftTitleLbl.attributedText = mutableAttriStr;
}else {
NSMutableAttributedString *mutableAttriStr = [Self getNoStrikethroughAttribute:text andTextColor:UIColorFromRGB(0x000000)];
cell.leftTitleLbl.attributedText = mutableAttriStr;
}https://stackoverflow.com/questions/58142205
复制相似问题