首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >strikeThrough属性与iOS 13不兼容。

strikeThrough属性与iOS 13不兼容。
EN

Stack Overflow用户
提问于 2019-09-27 23:25:35
回答 4查看 1.2K关注 0票数 1

我使用了这个String扩展将strikeThrough添加到文本中

这是分机:

代码语言:javascript
复制
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

代码语言:javascript
复制
     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当前的表单不起作用。

非常感谢

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2019-09-28 02:51:44

您可以在控制器中定义strikeThrough,而不是使用扩展。

例如:

代码语言:javascript
复制
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,只需将其颜色更改为清除.

票数 0
EN

Stack Overflow用户

发布于 2019-11-05 17:49:43

我让它在设置值为0的情况下工作,如果我不想再显示它。

代码语言:javascript
复制
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))
票数 2
EN

Stack Overflow用户

发布于 2019-11-04 11:21:46

我在iOS13上遇到了同样的问题,我在tableviewCell中使用了attrbutedText,如下所示:

代码语言:javascript
复制
  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显示。

为此,我试着为细胞制作两种标识符,并且正常工作。

代码语言:javascript
复制
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" ];
}
...

解决这个问题的另一种更优雅的方法是:

代码语言:javascript
复制
- (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;
        }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58142205

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档