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

现在我想要红色的 Rs.0 字符串...如何为该特定字符串赋予红色???

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

最佳答案

您可以在 iOS 6 及更高版本中使用 NSMutableAttributedString。

示例代码:

    NSString *strFirst = @"Multislice CT Scan Virtual Bronchoscopy";
    NSString *strSecond = @"Rs.0";

    NSString *strComplete = [NSString stringWithFormat:@"%@\n %@",strFirst,strSecond];

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

    [attributedString addAttribute:NSForegroundColorAttributeName
                  value:[UIColor redColor]
                  range:[strComplete rangeOfString:strSecond]];


   cell.textLabel.attributedText = attributedString;

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

相关文章:

android - 使用 Firebase 进行应用链接跟踪

iphone - 需要将数据从 Bundle 加载到我的 iPhone 应用程序中

ios - 在不知道高度的情况下在单元格内异步下载图像

objective-c - 有什么理由不返回一个不期望的可变对象吗?

ios - 如何注意 UITableViewCell 是否离开了可见区域?

ios - 使用日期字符串Swift解析大型json数据时的CPU使用率高

ios - Xamarin iOS 使用 Jenkins 和 Nuget 在 OSX 上构建服务器

ios - 验证一个无效的 NSTimer

ios - 从复杂的 ScrollView 页面移动时崩溃

ios - 如何使用分页更改 tableview 自定义单元格中 imageview 的图像。