我正在使用一个标签,该标签显示具有删除线属性的产品的旧价格。我试图为属性字符串设置删除线属性,但无法获得实际结果。
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!)
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 above
attributedString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: NSMakeRange(0, attributedString.length))
// Swift 4.1 and below
attributedString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: 2, range: NSMakeRange(0, attributedString.length))
oldPriceLabel.attributedText = attributedString
}
结果:
₹1,000.00
对于Swift 2:
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 above
attributedString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: NSMakeRange(0, attributedString.length))
// Swift 4.1 and below
attributedString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributedString.length))
self.oldPriceLabel.attributedText = attributedString
迅速
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your Text")
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length))
在这里,您可以提到一系列文本和删除线所需的字体大小
let dateText = NSMutableAttributedString.init(string: "This is a test string.")
dateText.setAttributes([NSAttributedStringKey.font: UIFont(name: "Poppins-Regular", size: 11)!,
NSAttributedStringKey.foregroundColor: UIColor.black, NSAttributedString.Key.strikethroughStyle: 2],
range: NSMakeRange(0, 8))
dateText.setAttributes([NSAttributedStringKey.font: UIFont(name: "Poppins-SemiBold", size: 18)!,
NSAttributedStringKey.foregroundColor: UIColor.black, NSAttributedString.Key.strikethroughStyle: 4],
range: NSMakeRange(10, 13))
dateText.setAttributes([NSAttributedStringKey.font: UIFont(name: "Poppins-SemiBold", size: 15)!,
NSAttributedStringKey.foregroundColor: UIColor.black, NSAttributedString.Key.strikethroughStyle: 1],
range: NSMakeRange(14, 17))
// set the attributed string to the UILabel object
deadline_lbl.attributedText = dateText