繁体   English   中英

如何从 NSAttributedString swift 中删除属性?

[英]How to remove attributes from the NSAttributedString swift?

我在我的按钮中添加了一些属性 attributedTitle

 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 ?

使用removeAttribute方法:

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

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

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

在你的情况下

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

在斯威夫特 5

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM