let price = 1000.0let currencyFormatter = NumberFormatter()currencyFormatter.numberStyle = .currencycurrencyFormatter.currencyCode = "INR"let priceInINR = currencyFormatter.string(from: price as NSNumber)let attributedString = NSMutableAttributedString(string: priceInINR!) attributedString.addAttribute(NsstrikethroughStyleAttributename,value: 1,range: NSMakeRange(0,attributedString.length)) self.oldPriceLabel.attributedText = attributedString
有没有办法同时为字符串设置货币格式化程序和删除线属性?
解决方法 试试这个并看看(Swift 3& 4兼容):@IBOutlet var oldPriceLabel: UILabel!func strikeOnLabel(){ let price = 1000.0 let currencyFormatter = NumberFormatter() currencyFormatter.numberStyle = .currency currencyFormatter.currencyCode = "INR" let priceInINR = currencyFormatter.string(from: price as NSNumber) let attributedString = NSMutableAttributedString(string: priceInINR!) // Swift 4.2 and aboveattributedString.addAttribute(NSAttributedString.Key.strikethroughStyle,value: 2,attributedString.length))// Swift 4.1 and belowattributedString.addAttribute(NSAttributedStringKey.strikethroughStyle,attributedString.length)) oldPriceLabel.attributedText = attributedString}
结果:
₹1,000.00
对于Swift 2:
let price = 1000.0let currencyFormatter = NumberFormatter()currencyFormatter.numberStyle = .currencycurrencyFormatter.currencyCode = "INR"let priceInINR = currencyFormatter.string(from: price as NSNumber)let attributedString = NSMutableAttributedString(string: priceInINR!)// Swift 4.2 and aboveattributedString.addAttribute(NSAttributedString.Key.strikethroughStyle,attributedString.length))// Swift 4.1 and belowattributedString.addAttribute(NsstrikethroughStyleAttributename,attributedString.length))self.oldPriceLabel.attributedText = attributedString总结
以上是内存溢出为你收集整理的ios – 在swift 3中为货币格式化字符串设置删除线属性文本全部内容,希望文章能够帮你解决ios – 在swift 3中为货币格式化字符串设置删除线属性文本所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)