菜鸟教程小白 发表于 2022-12-12 16:49:00

ios - 更改属性 TextView 中一个单词的颜色(objective-c)


                                            <p><p>我有一个带有属性文本的 TextView 。我正在尝试将属性文本的前 14 个字母(“隐私政策”)的颜色更改为存储值“appTintColor”。此应用程序的色调颜色存储在 p 列表中,并且针对每个应用程序目标具有不同的颜色。</p>

<p>如何将 textView 属性文本的前 5 个字母更改为这个变量“appTintColor”?我知道我可以使用 xCode 界面轻松地将颜色更改为 RGB/HEX 颜色...但我想将此颜色更改为基于应用目标的动态变量“appTintColor”。</p>

<p>我是否在文本字段中添加一个导出,为前 14 个字符添加下标,并将其设置为 appTintColor?这就是我的逻辑,但我似乎无法在代码中得到它。</p>

<p> <a href="/image/bZIMV.png" rel="noreferrer noopener nofollow"><img src="/image/bZIMV.png" alt="enter image description here"/></a> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>你需要一个 <code>NSMutableAttributedString</code> - 试试下面的代码:</p>

<pre><code>UIColor *color = ;

NSString *privacyPolicyFullStr = @&#34;Privacy policy\n blah stuff things blah cat bat mongoose platypus&#34;;

NSMutableAttributedString *mutAttrStr = [initWithString:privacyPolicyFullStr attributes:nil];

NSString *privacyPolicyShortStr = @&#34;Privacy Policy&#34;; //just to avoid using a magic number
NSDictionary *attributes = @{NSForegroundColorAttributeName:color};
;

self.textView.attributedText = mutAttrStr;
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 更改属性 TextView 中一个单词的颜色(objective-c),我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/33270761/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/33270761/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 更改属性 TextView 中一个单词的颜色(objective-c)