Skip to content

Instantly share code, notes, and snippets.

@hanfengs
Last active December 6, 2019 09:15
Show Gist options
  • Save hanfengs/4540f0c8e46070caeedeea575e17abb0 to your computer and use it in GitHub Desktop.
Save hanfengs/4540f0c8e46070caeedeea575e17abb0 to your computer and use it in GitHub Desktop.
[Label显示不同颜色] UILabel show different colors,根据range给的位置,让范围内的Label文字设置颜色 #Label
[self returnColorString:@"hello, i am xiao qu" WithRange:@"7 7,9 10,17 18"];//, 9 10
self.dialogLab.attributedText = [self returnColorString:resultDic.refText WithRange:resultDic.scoreInfo];
- (NSMutableAttributedString *)returnColorString:(NSString *)oldString WithRange:(NSString *)range{
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:oldString];
NSArray *arr = [range componentsSeparatedByString:@","];
for (NSString *rang in arr) {
NSArray *subArr = [rang componentsSeparatedByString:@" "];
NSInteger start = [subArr.firstObject integerValue];
NSInteger end = [subArr.lastObject integerValue];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(start, (end - start + 1))];
}
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment