NSMutableAttributedString 在具有特定范围的 tableviewcell 中不起作用

NSMutableAttributedString not working in tableviewcell with a specific range

我关注了,但没有解决我的问题。

我在ViewController中有一个tableviewtableviewcell有一个标签。我想用 NSStrikethroughStyleAttributeName 设置 attributedString。如果我将完整的字符串设置为删除线,它会起作用 但是 如果我将其设置为部分字符串,它就不起作用。

Below code for success result

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 

Below code not working

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