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

标签 ios objective-c textview

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

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

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

enter image description here

最佳答案

您需要一个 NSMutableAttributedString - 尝试以下代码:

UIColor *color = [UIColor greenColor];

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

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

NSString *privacyPolicyShortStr = @"Privacy Policy"; //just to avoid using a magic number
NSDictionary *attributes = @{NSForegroundColorAttributeName:color};
[mutAttrStr setAttributes:attributes range:NSMakeRange(0, privacyPolicyShortStr.length)];

self.textView.attributedText = mutAttrStr; 

关于ios - 更改属性 TextView 中一个单词的颜色(objective-c),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33270761/

相关文章:

ios - 更改 MPMoviePlayerController 的内容

iphone - iAd 横幅问题

android - 如何将更大的文本设置为宽度为 wrap_content 的 textview?

android - 如何以编程方式将 TextView 更改为 ImageView 并返回

ios - 以编程方式设置文本时,不会触发 UITextField 委托(delegate)方法

ios - 如何检查 NSMutableArray 中的状态

objective-c - 如何定义和发送将 UIBarButtonItems 动态添加到 UIToolbar 的方法\消息?

objective-c - 为什么不调用 UIPinchGestureRecognizer 而 scrollViewDidEndZooming 是?

android - 如何在html段落中添加@string?

c# - 获取 UIView 的大小