先上效果圖:
效果圖
最近在做的東西涉及到給文字添加刪除線,參考以前富文本添加下劃線的做法改了變量名之后一直還是沒有出來,發現需要添加
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