OGeek|极客世界-中国程序员成长平台

标题: ios - NSMutableAttributedString 在特定范围的 tableviewcell 中不起作用 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 15:21
标题: ios - NSMutableAttributedString 在特定范围的 tableviewcell 中不起作用

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

我在 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 



Best Answer-推荐答案


试试这个,

   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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4