先上效果图:
最近在做的东西涉及到给文字添加删除线,参考以前富文本添加下划线的做法改了变量名之后一直还是没有出来,发现需要添加
NSBaselineOffsetAttributeName
。如下
let previousPrice = "$" + String(product.unitPrice / product.discount)
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: previousPrice, attributes: [NSFontAttributeName:UIFont(name: "HelveticaNeue-Light", size: 11.0)!])
attributeString.addAttribute(NSBaselineOffsetAttributeName, value: 0, range: NSMakeRange(0, attributeString.length))
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 1, range: NSMakeRange(0, attributeString.length))
attributeString.addAttribute(NSForegroundColorAttributeName, value: UIColor.lightGray, range: NSRange(location:0,length:attributeString.length))
let currentPrice : NSMutableAttributedString = NSMutableAttributedString(string: productPrice + " ")
currentPrice.append(attributeString)
cell.productPriceLabel.attributedText = currentPrice