【问题标题】:How Do I create an "underline" text in UITableViewCell?如何在 UITableViewCell 中创建“下划线”文本?
【发布时间】:2018-11-16 20:39:45
【问题描述】:

我必须创建一个 UITableView 单元,其中有一条“线”通过。你们知道怎么做吗?这将是一个价格标签,所以我想说:“旧价格,现在有 50% 的折扣”。所以,“旧价格”应该被跨越。

界面代码

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "topSoldCell", for: indexPath) as! TableViewCell

    let content = self.dataTopSold[indexPath.item]
    cell.labelNomeTopSell.text = content.nome
    cell.imageViewTopSell.setImage(url: content.urlImagem, placeholder: "")
    cell.labelPrecoDe.text = "R$ \(content.precoDe)"
    cell.labelPrecoPor.text = "R$ 119.99"
    return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    performSegue(withIdentifier: "segueId", sender:self.dataTopSold[indexPath.row])

    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

         if segue.identifier == "segueId" {

        let des = segue.destination as! TelaDetalheProdutos

        des.stringNomeeDesc = sender as! Top10
    }
}

【问题讨论】:

  • 显示一些用户界面或代码
  • 我已经尝试了这篇文章的所有方法。但是,一旦我想要它在我的 UITableview Cell @AkshaysinghYaduvanshi 上,它们都不适合我
  • @Anbu.karthik 你好!我刚刚更新了包含 UICode 的帖子。请检查一下好吗?

标签: ios swift uitableview uilabel


【解决方案1】:

你的情况

It will be a price tag, so I want to say something like: "Old price and now with 50% OFF". So, the "old price" should be crossed.

您可以使用属性字符串来支持它。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "topSoldCell", for: indexPath) as! TableViewCell
    let content = self.dataTopSold[indexPath.item]
    cell.labelNomeTopSell.text = content.nome
    cell.imageViewTopSell.setImage(url: content.urlImagem, placeholder: "")
    let oldPrice = "R$ \(content.precoDe)"
    let promotionString = oldPrice + " and now with 50% OFF"
    let attributedStr = NSMutableAttributedString(string: promotionString)
    let crossAttr = [NSAttributedStringKey.strikethroughStyle: NSUnderlineStyle.styleSingle.rawValue]
    attributedStr.addAttributes(crossAttr, range: NSMakeRange(0, oldPrice.count))
    cell.labelPrecoDe.text = attributedStr
    cell.labelPrecoPor.text = "R$ 119.99"
    return cell
}

【讨论】:

  • 您好!哦,我想我写错了。当我说“旧价格”时,我的意思是字面上的旧产品价格。我必须在我的 UITabeviewCell 上使用 UILabel 来接收旧产品价格。你找到我了吗?
  • @BrunoVavretchek 将“旧价格”更改为“(your_old_price)”,将 yourLabel 更改为 UITableviewCell 的标签
  • 所以,“旧价格”会收到 cell.labelOldPrice.text?。这行得通吗?
  • 你能看看我的代码并提供一个例子吗?
  • @BrunoVavretchek : 更新了我的代码,你能检查一下吗?
【解决方案2】:

由于旧价格是静态内容,也是标签的首位。所以你可以把线条图像放在第一位,让它看起来像 Old price 。现在您可以根据需要隐藏/显示图像了。

如果您必须为标签中的某些文本添加下划线,那么只需将该字符串放入 TextEdit。根据需要编辑它们。并通过将标签样式 Plain 设置为 Attributed 来复制到标签文本。

希望这对你有用

【讨论】:

    猜你喜欢
    • 2017-01-29
    • 2014-04-18
    • 2017-01-18
    • 2023-04-05
    • 2015-08-22
    • 2013-02-21
    • 1970-01-01
    • 1970-01-01
    • 2015-07-07
    相关资源
    最近更新 更多