如何创建"下划线" UITableViewCell中的文字?

时间:2018-06-07 03:45:17

标签: ios swift uitableview uilabel

我必须使用" line"创建一个UITableView Cell。路过。你们知道怎么做吗?这将是一个价格标签,所以我想说的是:"旧价格,现在有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
    }
}

2 个答案:

答案 0 :(得分:0)

旧价格是静态内容,也是标签中的第一个位置。因此,您可以拍摄线条图像并在第一个位置放置标签,以便它看起来像旧价格。现在,您可以根据需要隐藏/显示图像。

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

希望这对你有用

答案 1 :(得分:0)

在你的情况下

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
}