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

3

6

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?

Rainfall answered 30/8, 2017 at 12:14 Comment(3)
Please add CodePropjet
which type text you give exampleLohman
Please edit your question to show the code you have so far. You should include at least an outline (but preferably a minimal reproducible example) of the code that you are having problems with, then we can try to help with the specific problem. You should also read How to Ask.Peralta
I
7

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
Impersonalize answered 30/8, 2017 at 14:58 Comment(3)
Thanks for the reply. It says, NSAttributedStringKey.strikethroughStyle is not compatible.Rainfall
I test this in and obtained result, what I've shown here. I can share you project file, if you want to see. Share me you contact (email)Impersonalize
Actually the problem occurs only with iOS 10.x. It works fine in 9.x.Rainfall
L
1

swift

let attributeString: NSMutableAttributedString =  NSMutableAttributedString(string: "Your Text")
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length))
Lohman answered 30/8, 2017 at 12:25 Comment(0)
L
0

Here you can mention the fontsize required for a range of text along with the strikethriough

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
Larsen answered 28/12, 2018 at 10:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.