ios - 如何将UIcolor分配给NSString?

标签 ios objective-c uitableview nsstring uicolor

我有字典数组

[
        {
          "price": 0,
          "test_name": "Multislice CT Scan Virtual Bronchoscopy"
        },
        {
          "price": 0,
          "test_name": "Multislice CT Scan Dental"
        },
        {
          "price": 0,
          "test_name": "Multislice CT Scan Joint (3D)"
        },
        {
          "price": 0,
          "test_name": "Multislice CT Scan Lumbar"
        },
        {
          "price": 0,
          "test_name": "Multislice CT Scan Spine - Cervical / Thoracic"
        },
        {
          "price": 0,
          "test_name": "Multislice CT Scan CT Urography"
        }
]


然后将这个Dictionary数组转换为NSString,就像在每个字典值的末尾附加\ n一样

Multislice CT Scan Virtual Bronchoscopy  
Rs.0

Multislice CT Scan Dental  
Rs.0

Multislice CT Scan Joint (3D)  
Rs.0

Multislice CT Scan Lumbar  
Rs.0

Multislice CT Scan Spine - Cervical / Thoracic  
Rs.0

Multislice CT Scan CT Urography  
Rs.0

Multislice CT Scan (Arterial + Portal + Venous)  
Rs.0

Multislice CT Scan Abdomen Triphasic Lever  
Rs.0


现在我想在RedColor中使用Rs.0字符串...如何为该特定字符串赋予红色?

正在使用静态表..将整个字符串分配给单个标签。

最佳答案

尝试使用这种方法,所有Rs.0将显示为红色

NSString *str = @"Multislice CT Scan Virtual Bronchoscopy Rs.0 Multislice CT Scan Dental Rs.0";

NSMutableAttributedString *attributedString =[[NSMutableAttributedString alloc] initWithString:str];

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(Rs.[0-9]*)" options:kNilOptions error:nil];

NSRange range = NSMakeRange(0,strFirst.length);

[regex enumerateMatchesInString:strFirst options:kNilOptions range:range usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {

    NSRange subStringRange = [result rangeAtIndex:1];
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:subStringRange];
}];
self.label.attributedText = attributedString;


希望这会帮助你。

关于ios - 如何将UIcolor分配给NSString?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37893776/

相关文章:

ios - 从以编程方式创建的导航栏项设置操作

ios - 可以在启动相机应用程序时控制白平衡模式吗? (iOS)

iPhone + UITableView + 访问特定行的单元格

ios - Segue 以查看 Controller 崩溃

ios - 如何检查设备是否可以调用电话(iOS 8)?

ios - 使用Regex检查Double值是否具有特定格式

objective-c - 如何让UIView的两个动画一个一个执行

ios - Objective-C 继承重复代码问题

Objective-C:阻塞线程直到 NSTimer 完成(iOS)

ios - 如何使用多级部分正确显示 uitableview 中的嵌套数组数据