Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Strikethrough Attribute text for a currency formatted string in swift 3

I am working with a label which shows the old price of a product with strikethrough attribute. I am trying to set the strikethrough property for the attributed string but could not get the actual result.

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

Is there any way to set the currency formatter and strikethrough attributes simultaneously for a string?

like image 635
Mahesh Avatar asked Aug 30 '17 12:08

Mahesh


People also ask

How do you strikethrough in Swift?

Swift5 UILabel Extension.Change the text property to attributed and select the text and right click to get the font property. Click on the strikethrough.

What is NSAttributedString in Swift?

An NSAttributedString object manages character strings and associated sets of attributes (for example, font and kerning) that apply to individual characters or ranges of characters in the string. An association of characters and their attributes is called an attributed string.

How do you bold a string in Swift?

Usage: let label = UILabel() label. attributedText = NSMutableAttributedString() . bold("Address: ") .


2 Answers

Try this and see (Swift 3 & 4 compatible):

@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
}

Result:

₹1,000.00


For 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
like image 88
Krunal Avatar answered Oct 20 '22 22:10

Krunal


swift

let attributeString: NSMutableAttributedString =  NSMutableAttributedString(string: "Your Text")
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length))
like image 20
Jay Avatar answered Oct 20 '22 20:10

Jay