NSMutableAttributedString在tableviewcell中不具有特定范围 [英] NSMutableAttributedString not working in tableviewcell with a specific range

查看:124
本文介绍了NSMutableAttributedString在tableviewcell中不具有特定范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已关注这个问题但没有解决我的问题。

I have followed this question but didn't solve my problem.

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

I have a tableview in ViewController , tableviewcell have a label. I want to set attributedString with NSStrikethroughStyleAttributeName. If i am setting complete string as a strikethrough it works but if i am setting as partial it doesn't works.


下面的成功结果代码

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

这篇关于NSMutableAttributedString在tableviewcell中不具有特定范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆