我遵循了this question,但没有解决我的问题。
我在ViewController中有一个tableview
,tableviewcell
有一个标签。我想用attributedString
设置NSStrikethroughStyleAttributeName
。如果我将完整的字符串设置为删除线,则可以使用,但如果我将stringt设置为不完整,则可以使用。
以下代码显示成功结果
let strOriginalPrice = "my price"
let strdiscountedPrice = "discounted price"
let strPrice = strOriginalPrice+" "+strdiscountedPrice
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: strPrice)
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length))
cell.lblPrice.attributedText = attributeString
下面的代码不起作用
let attributedDiscunt: NSMutableAttributedString = NSMutableAttributedString(string: strPrice)
attributedDiscunt.addAttribute(NSStrikethroughStyleAttributeName, value:2, range: NSMakeRange(0, strOriginalPrice.characters.count-1))
cell.lblPrice.attributedText = attributedDiscunt
最佳答案
试试这个,
let strOriginalPrice = "my price"
let strdiscountedPrice = "discounted price"
let strPrice = strOriginalPrice+" "+strdiscountedPrice
// let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: strPrice)
// attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length))
let attributedDiscunt: NSMutableAttributedString = NSMutableAttributedString(string: strPrice)
attributedDiscunt.addAttribute(NSStrikethroughStyleAttributeName, value:2, range: NSMakeRange(0, strOriginalPrice.characters.count-1))
attributedDiscunt.addAttribute(NSBaselineOffsetAttributeName, value: 0, range: NSMakeRange(0, strOriginalPrice.characters.count-1))
cell.lblPrice.attributedText = attributedDiscunt
关于ios - NSMutableAttributedString在具有特定范围的tableviewcell中不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43997643/