【问题标题】:How to remove attributes from the NSAttributedString swift?如何从 NSAttributedString swift 中删除属性?
【发布时间】:2015-11-19 14:07:02
【问题描述】:

我为按钮属性标题添加了一些属性

 let attr = NSMutableAttributedString(string: currTitle)

 attr.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attr.length))
 attr.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(0,  attr.length))

 currButton?.setAttributedTitle(attr, forState: UIControlState.Normal)

点击按钮后如何删除 NSStrikethroughStyleAttributeName?

【问题讨论】:

  • 哇。这就是我感兴趣的问题。谢谢你的问题。这是奇迹。请分享一下
  • 不客气 PiraTa...谢谢! :)

标签: ios swift nsattributedstring setattribute nsmutableattributedstring


【解决方案1】:

使用removeAttribute 方法:

attr.removeAttribute(NSStrikethroughStyleAttributeName, range: NSMakeRange(0, attr.length))

【讨论】:

  • 我想删除罢工数据,当我点击一个按钮时,但通过这段代码,我总是得到未删除的按钮文本
【解决方案2】:

这很简单。您可以在NSMutableAttributedString 类中使用此方法

func removeAttribute(_ name: String,
               range range: NSRange)

你的情况

attr.removeAttribute(NSStrikethroughStyleAttributeName , range:NSMakeRange(0, attr.length))

【讨论】:

    【解决方案3】:

    在 Swift 5 中

    let attributeString: NSMutableAttributedString =  NSMutableAttributedString(string: "YourStringHere")
    attributeString.removeAttribute(NSAttributedString.Key.strikethroughStyle, range: NSMakeRange(0, attributeString.length))
    yourLblHere.attributedText = attributeString
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-18
      • 1970-01-01
      • 2011-05-09
      • 2018-10-05
      相关资源
      最近更新 更多