【问题标题】:How to assign UIcolor to NSString?如何将 UIcolor 分配给 NSString?
【发布时间】:2016-10-20 00:17:45
【问题描述】:

我有字典数组

[
        {
          "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 字符串为 redColor...如何为该特定字符串赋予红色???

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

【问题讨论】:

  • 您可以使用NSMutableAttributedString
  • 为什么商品名称和价格需要在同一个字符串中?将它们显示在单独的标签控件中,并使用attributedText 属性为标签着色。
  • 它是一个静态单元格...如果我使用两个标签,那么值必须动态分配对吗?这就是为什么我使用单个标签...和单个字符串来分配两个值

标签: ios objective-c uitableview nsstring uicolor


【解决方案1】:

您可以在 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;

【讨论】:

    【解决方案2】:

    NSMutableAttributedString 你在找什么。

    试试这个和示例代码

     NSString *strFirst = @"Multislice CT Scan Virtual Bronchoscopy";
        NSString *strSecond = @"Rs.0";
    
    
        NSString *strComplete = [NSString stringWithFormat:@"%@ %@",strFirst,strSecond];
    
        NSMutableAttributedString *attributedString =[[NSMutableAttributedString alloc] initWithString:strComplete];
        //you can change your string color as your need like below
        [attributedString addAttribute:NSForegroundColorAttributeName
                                 value:[UIColor blackColor]
                                 range:[strComplete rangeOfString:strFirst]];
    
        [attributedString addAttribute:NSForegroundColorAttributeName
                                 value:[UIColor redColor]
                                 range:[strComplete rangeOfString:strSecond]];
    
    
    
        self.yourLbl.attributedText = attributedString;
    

    【讨论】:

    • 正在使用 for 循环将字典数组转换为 nsstring... packageString = [NSString stringWithFormat:@"%@%@\nRs.%@\n\n",packageString,[package objectForKey: @"package_name"],[package objectForKey:@"price"]];如何在循环中应用它...我已经应用了,但是我收到了 NSInvalidArgumentException [__NSCFNumber length] 的错误:发送了无法识别的选择器
    【解决方案3】:

    使用这个尝试这样的事情,你的所有 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;
    

    希望这会对你有所帮助。

    【讨论】:

    • @Akshay 你试试我的答案。如果您不明白,请随时提出任何问题。
    • 如果价格为 100 卢比,那么它不会显示红色... 对此的调节器表达式是什么?
    • 好的,让我试试。
    • @Akshay 我已经编辑了答案并将模式Rs. 0 更改为(Rs.[0-9]*)。现在试试看,它会转换任何红色的卢比数字。
    • 为什么商品名称和价格需要在同一个字符串中?如果这是呈现给用户,那么它们可以是单独的标签控件,您可以根据需要使用价格标签的attributedText 进行着色。使用组合文本创建一个字符串,然后使用正则表达式再次查找不同的部分是一个真正可怕的解决方案。你会有很多错误,而且看起来很糟糕。
    【解决方案4】:

    使用这个方法:

    -(UIColor *)giveColorfromStringColor:(NSString *)colorname
    {
        SEL labelColor = NSSelectorFromString(colorname);
        UIColor *color = [UIColor performSelector:labelColor];
        return color;
    }
    

    【讨论】:

    • 从哪里调用这个方法?
    • 你在标签中分配然后像这样使用它:self.textLabel.textColor = [self giveColorfromStringColor:@"redColor"];
    【解决方案5】:

    试试这个代码

    - (NSString *)stringFromColor:(UIColor *)color
    {
        const size_t totalComponents = CGColorGetNumberOfComponents(color.CGColor);
        const CGFloat * components = CGColorGetComponents(color.CGColor);
        return [NSString stringWithFormat:@"#%02X%02X%02X",
                (int)(255 * components[MIN(0,totalComponents-2)]),
                (int)(255 * components[MIN(1,totalComponents-2)]),
                (int)(255 * components[MIN(2,totalComponents-2)])];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-24
      相关资源
      最近更新 更多