0

I try to implement strikethroughStyle to labels. I use this below code for one label. But I want to draw one line for this two different label as in picture . (I use stackview for these labels)

Thanks in advance.

let attributedString2 = NSMutableAttributedString(string: self.productDetailView.productOldLastPriceLabel.text ?? "")
attributedString2.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: NSMakeRange(0, attributedString2.length))
self.productDetailView.productOldLastPriceLabel.attributedText = attributedString2

enter image description here

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
Ali Ihsan URAL
  • 1,894
  • 1
  • 20
  • 43

2 Answers2

2

Use the below code to manage strikethrough with different font size of text. You can modify the range as per your requirement

let str1 = "16230"
let str2 = "63455333"

let dateText = NSMutableAttributedString.init(string: "\(str1)\(str2)")
    dateText.setAttributes([NSAttributedString.Key.font: UIFont.systemFont(ofSize: 34, weight: UIFont.Weight.bold),
                            NSAttributedString.Key.foregroundColor: UIColor.black, NSAttributedString.Key.strikethroughStyle: 2],
                           range: NSMakeRange(0, str1.count))
    dateText.setAttributes([NSAttributedString.Key.font: UIFont.systemFont(ofSize: 34, weight: UIFont.Weight.thin),
                            NSAttributedString.Key.foregroundColor: UIColor.black, NSAttributedString.Key.strikethroughStyle: 1],
                           range: NSMakeRange(str1.count,str2.count))

    // set the attributed string to the UILabel object
    deadline_lbl.attributedText = dateText

screenshot

Yogesh Tandel
  • 1,738
  • 1
  • 19
  • 25
0

You can use just one label to write all text istead of two adjancted texts. And use different fonts for its parts and use strikethroughStyle for all text.

This link could help you to do this

Ekrem Duvarbasi
  • 189
  • 1
  • 7