一个删除线样式中的Swift多标签

一个删除线样式中的Swift多标签,swift,uilabel,uistackview,Swift,Uilabel,Uistackview,我尝试对标签实现删除线样式。我使用下面的代码作为一个标签。但我想为这两个不同的标签画一条线,如图所示。(我对这些标签使用stackview) 提前谢谢 let attributedString2 = NSMutableAttributedString(string: self.productDetailView.productOldLastPriceLabel.text ?? "") attributedString2.addAttribute(NSAttributedString.Key.st

我尝试对标签实现删除线样式。我使用下面的代码作为一个标签。但我想为这两个不同的标签画一条线,如图所示。(我对这些标签使用
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

您只需使用一个标签,就可以在两个相邻文本的基础上书写所有文本。并对其部分使用不同的字体,对所有文本使用删除线样式


可以帮助您做到这一点

您只需使用一个标签就可以在两个相邻文本的基础上书写所有文本。并对其部分使用不同的字体,对所有文本使用删除线样式


可以帮助您做到这一点

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

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

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

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

我不知道字符串的长度如果没有长度,您如何知道应用什么字体大小和文本。你能分享一些代码让我检查我使用两个uilabel吗。我用小数部分和整数部分解析json字符串。然后我将这些标签与uistackviewHi合并。我已经修改了代码,以便您能够理解。用fontsize和strike through you requireyes将两个字符串的文本添加到datetext中,因为strike行取决于字体、is大小、重量等。如果没有长度,我不知道字符串的长度。您如何知道应用什么字体大小和文本。你能分享一些代码让我检查我使用两个uilabel吗。我用小数部分和整数部分解析json字符串。然后我将这些标签与uistackviewHi合并。我已经修改了代码,以便您能够理解。使用fontsize和Stroke through you requireyes将两个字符串的文本添加到datetext中,因为Stroke行取决于字体、is大小、重量等。