【问题标题】:Swift Multiple Label in one strikethroughStyle一个删除线样式中的 Swift 多个标签
【发布时间】:2018-12-27 14:41:01
【问题描述】:

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

【问题讨论】:

    标签: swift uilabel uistackview


    【解决方案1】:

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

    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 。我用小数部分和 Int 部分解析 json 字符串。然后我将这些标签与 uistackview 合并
    • 您好我已经修改了代码以便您理解。将 2 个字符串的文本添加到 datetext 中,并按您需要的字体大小删除
    • 是的,因为罢工线将取决于字体、大小、重量等。
    【解决方案2】:

    您可以只使用一个标签来编写所有文本,而不是两个附加文本。并对其部分使用不同的字体,并对所有文本使用删除线样式。

    This link 可以帮你做到这一点

    【讨论】:

      猜你喜欢
      • 2016-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-21
      • 1970-01-01
      • 2017-05-23
      • 2015-10-24
      • 1970-01-01
      相关资源
      最近更新 更多