首页 > 解决方案 > 一个删除线样式中的 Swift 多个标签

问题描述

我尝试对标签实施删除线样式。我将以下代码用于一个标签。但我想为这两个不同的标签画一条线,如图所示。(我stackview用于这些标签)

提前致谢。

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

在此处输入图像描述

标签: swiftuilabeluistackview

解决方案


使用以下代码管理具有不同字体大小的文本的删除线。您可以根据需要修改范围

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

截屏


推荐阅读