我有一个带有属性文本的 TextView 。我正在尝试将属性文本的前 14 个字母(“隐私政策”)的颜色更改为存储值“appTintColor”。此应用程序色调存储在 p 列表中,并且针对每个应用程序目标具有不同的颜色。
如何将 textView 属性文本的前 5 个字母更改为此变量“appTintColor”?我知道我可以使用 xCode 界面轻松地将颜色更改为 RGB/HEX 颜色...但我想将此颜色更改为变量“appTintColor”,该变量是基于应用程序目标的动态变量。
我是否要向文本字段添加一个导出,为前 14 个字符添加下标,并将其设置为 appTintColor?这是我的逻辑,但我似乎无法在代码中得到它。
最佳答案
您需要一个 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/