如何将 UIcolor 分配给 NSString?
How to assign UIcolor to NSString?
我有字典数组
[
{
"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 红色字符串...如何为该特定字符串赋予红色???
我正在使用静态 table.. 因为我将整个字符串分配给单个标签..
NSMutableAttributedString what you looking for.
试试这个和示例代码
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;
您可以在 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;
使用这个方法:
-(UIColor *)giveColorfromStringColor:(NSString *)colorname
{
SEL labelColor = NSSelectorFromString(colorname);
UIColor *color = [UIColor performSelector:labelColor];
return color;
}
使用这个尝试类似的操作,您所有的 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;
希望对您有所帮助。
试试这个代码
- (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)])];
}
我有字典数组
[
{
"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
,就像在每个字典值
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 红色字符串...如何为该特定字符串赋予红色???
我正在使用静态 table.. 因为我将整个字符串分配给单个标签..
NSMutableAttributedString what you looking for.
试试这个和示例代码
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;
您可以在 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;
使用这个方法:
-(UIColor *)giveColorfromStringColor:(NSString *)colorname
{
SEL labelColor = NSSelectorFromString(colorname);
UIColor *color = [UIColor performSelector:labelColor];
return color;
}
使用这个尝试类似的操作,您所有的 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;
希望对您有所帮助。
试试这个代码
- (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)])];
}