Swift 一个删除线样式中的多个标签
Swift Multiple Label in one strikethroughStyle
我尝试对标签实施删除线样式。我将下面的代码用于一个标签。但我想为这两个不同的标签画一条线,如图所示。 (我对这些标签使用 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
您可以只使用一个标签来书写所有文本,而不是两个相邻的文本。并为其部分使用不同的字体,并为所有文本使用删除线样式。
This link 可以帮助您做到这一点
使用下面的代码来管理不同字体大小的文本的删除线。您可以根据需要修改范围
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
我尝试对标签实施删除线样式。我将下面的代码用于一个标签。但我想为这两个不同的标签画一条线,如图所示。 (我对这些标签使用 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
您可以只使用一个标签来书写所有文本,而不是两个相邻的文本。并为其部分使用不同的字体,并为所有文本使用删除线样式。
This link 可以帮助您做到这一点
使用下面的代码来管理不同字体大小的文本的删除线。您可以根据需要修改范围
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