ios - NSMutableAttributedString 在特定范围的 tableviewcell 中不起作用
<p><p>我关注了<a href="https://stackoverflow.com/questions/43074652/ios-10-3-nsstrikethroughstyleattributename-is-not-rendered-if-applied-to-a-sub" rel="noreferrer noopener nofollow">this question</a>但没有解决我的问题。</p>
<p>我在 ViewController 中有一个 <code>tableview</code> , <code>tableviewcell</code> 有一个标签。我想用 <code>NSStrikethroughStyleAttributeName</code> 设置 <code>attributedString</code>。如果我将完整的字符串设置为删除线,它可以工作<strong>但</strong>如果我设置为部分它不起作用。</p>
<blockquote>
<p>Below code for success result</p>
</blockquote>
<pre><code>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
</code></pre>
<blockquote>
<p>Below code not working</p>
</blockquote>
<pre><code>let attributedDiscunt: NSMutableAttributedString =NSMutableAttributedString(string: strPrice)
attributedDiscunt.addAttribute(NSStrikethroughStyleAttributeName, value:2, range: NSMakeRange(0, strOriginalPrice.characters.count-1))
cell.lblPrice.attributedText = attributedDiscunt
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>试试这个,</p>
<pre><code> 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
</code></pre></p>
<p style="font-size: 20px;">关于ios - NSMutableAttributedString 在特定范围的 tableviewcell 中不起作用,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/43997643/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/43997643/
</a>
</p>
页:
[1]