菜鸟教程小白 发表于 2022-12-11 19:37:43

ios - UILabel 不适用于 NSAttributedString + NSParagraphStyle 从右到左


                                            <p><p>当我使用 <code>UISemanticContentAttributeForceRightToLeft</code> 时,我遇到了使用 <code>NSAttributedStrings</code> 和 <code>NSParagraphStyle</code> 的 UILabel 问题。</p>

<p>我有一个 UI 上只有 2 个标签的演示应用。</p>

<p>它们有前导和尾随约束。</p>

<p> <a href="/image/dPsdU.png" rel="noreferrer noopener nofollow"><img src="/image/dPsdU.png" alt="The two labels"/></a> </p>

<p>我正在使用 </p> 在 AppDelegate 中从右到左制作应用程序

<pre><code>UIView.appearance().semanticContentAttribute = .forceRightToLeft
</code></pre>

<p>我只是用以下代码配置标签</p>

<pre><code>let labelAText = &#34;Foo&#34;

let mutAttrString = NSMutableAttributedString(string: labelAText)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .natural
paragraphStyle.baseWritingDirection = .natural

mutAttrString.setAttributes(, range: NSMakeRange(0, labelAText.count))

labelA.attributedText = mutAttrString
labelB.text = &#34;Bar&#34;
</code></pre>

<p>结果如下:</p>

<p>[ <img src="/image/n9Vcg.png" alt="wrong label"/> </p>

<p><code>labelB (Bar)</code> 按预期工作,但 <code>labelA (Foo)</code> 没有。如果我只是删除 <code>NSParagraphStyle</code> 它开始工作。请注意,我什至没有对其进行任何更改,但仅使用它就会弄乱对齐方式。</p>

<p>有什么可能导致这种情况的想法吗?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p> <a href="https://developer.apple.com/documentation/appkit/nsmutableparagraphstyle" rel="noreferrer noopener nofollow">NSMutableParagraphStyle</a>有特性<a href="https://developer.apple.com/documentation/uikit/nsmutableparagraphstyle/1534601-basewritingdirection" rel="noreferrer noopener nofollow">baseWritingDirection</a> (枚举 <a href="https://developer.apple.com/documentation/uikit/nswritingdirection" rel="noreferrer noopener nofollow">NSWritingDirection</a>)。设置为 <a href="https://developer.apple.com/documentation/uikit/nswritingdirection/1534029-righttoleft" rel="noreferrer noopener nofollow">rightToLeft</a> </p>

<p>试试这个</p>

<pre><code>paragraphStyle.baseWritingDirection = NSWritingDirection.rightToLeft
</code></pre>

<p><br/>
<strong>编辑</strong>:(希望对您有所帮助)</p>

<blockquote>
<p>If the value of property (baseWritingDirection) is NSWritingDirectionNaturalDirection, the receiver resolves the writing direction to either NSWritingDirectionLeftToRight or NSWritingDirectionRightToLeft, depending on the direction for the user’s language preference setting.</p>
</blockquote>

<pre><code>class func defaultWritingDirection(forLanguage languageName: String?) -&gt; NSWritingDirection
</code></pre>

<p><strong>languageName</strong>:以 ISO 语言区域格式指定的语言。可以为 nil 以返回从用户的默认数据库派生的默认书写方向。</p>

<p><br/>使用languageName = "ar"的解决方案</p>

<pre><code>@IBOutlet weak var labelA: UILabel!

let labelAText = &#34;Foo&#34;
let mutAttrString = NSMutableAttributedString(string: &#34;Foo&#34;)
let paragraphStyle = NSMutableParagraphStyle()
//paragraphStyle.alignment = .natural
paragraphStyle.baseWritingDirection = NSParagraphStyle.defaultWritingDirection(forLanguage: &#34;ar&#34;)
mutAttrString.setAttributes(, range: NSMakeRange(0, labelAText.count))

labelA.attributedText = mutAttrString
</code></pre>

<p>这里是语言列表:<a href="http://www.loc.gov/standards/iso639-2/php/English_list.php" rel="noreferrer noopener nofollow">Codes for the Representation of Names of Languages</a> </p>

<p><strong>注意:使用 <code>ISO 639-1</code> 代码作为语言。标签文本的方向取决于语言的自然方向。</strong></p>

<p>结果如下:</p>

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

<p><br/>
这份 Apple 文档将为您提供帮助 - <a href="https://developer.apple.com/library/content/documentation/MacOSX/Conceptual/BPInternational/SupportingRight-To-LeftLanguages/SupportingRight-To-LeftLanguages.html" rel="noreferrer noopener nofollow">Supporting Right-to-Left Languages</a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UILabel 不适用于 NSAttributedString &#43; NSParagraphStyle 从右到左,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/48326178/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/48326178/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UILabel 不适用于 NSAttributedString &#43; NSParagraphStyle 从右到左