菜鸟教程小白 发表于 2022-12-11 18:53:06

ios - 如何使用 UILabel 或 UITextview 实现文本的 LineSpacing 和背景颜色


                                            <p><p> <a href="/image/IHOC6.png" rel="noreferrer noopener nofollow"><img src="/image/IHOC6.png" alt="enter image description here"/></a> </p>

<p>如何在 swift 3 中制作一个 UILabel 或 UITextView 来实现这一点。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>这里有标签的解决方案。</p>

<pre><code>let label = UILabel()
let stringValue = &#34;How to implement LineSpacing and Background color of text with a UILabel&#34;
let attrString = NSMutableAttributedString(string: stringValue)
var style = NSMutableParagraphStyle()
style.lineSpacing = 24 // change line spacing between paragraph like 36 or 48
style.minimumLineHeight = 20 // change line spacing between each line like 30 or 40
attrString.addAttribute(NSParagraphStyleAttributeName, value: style, range: NSRange(location: 0, length: stringValue.characters.count))

// background color
attrString.addAttribute(NSBackgroundColorAttributeName, value: UIColor.red, range: NSRange(location: 0, length: stringValue.characters.count))

label.attributedText = attrString
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何使用 UILabel 或 UITextview 实现文本的 LineSpacing 和背景颜色,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/46541357/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/46541357/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何使用 UILabel 或 UITextview 实现文本的 LineSpacing 和背景颜色