ios – 在swift 3中为货币格式化字符串设置删除线属性文本

ios – 在swift 3中为货币格式化字符串设置删除线属性文本,第1张

概述我正在使用一个标签,该标签显示具有删除线属性的产品的旧价格.我试图为属性字符串设置删除线属性,但无法获得实际结果. let price = 1000.0let currencyFormatter = NumberFormatter()currencyFormatter.numberStyle = .currencycurrencyFormatter.currencyCode = "INR" 我正在使用一个标签,该标签显示具有删除线属性的产品的旧价格.我试图为属性字符串设置删除线属性,但无法获得实际结果.

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中为货币格式化字符串设置删除线属性文本所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1073309.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存