Ali Ihsan URAL
Ali Ihsan URAL

Reputation: 1974

Swift Multiple Label in one strikethroughStyle

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

Upvotes: 0

Views: 720

Answers (2)

Yogesh Tandel
Yogesh Tandel

Reputation: 1754

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

Upvotes: 2

Ekrem Duvarbasi
Ekrem Duvarbasi

Reputation: 187

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

Upvotes: -1

Related Questions