// @param text 如:h2o2 co2
// @param size 角标数字字体size
+(NSMutableAttributedString*)getNumberSubscriptString:(NSString*)text size:(CGFloat)size{
if (text.length == 0) {
return nil;
}
NSMutableArray *numTmpArray = [NSMutableArray array];
NSMutableAttributedString *resAttributed = [[NSMutableAttributedString alloc]initWithString:text.uppercaseString];
NSScanner *scanner = [NSScanner scannerWithString:text];
NSCharacterSet *numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
while (![scanner isAtEnd]) {
[scanner scanUpToCharactersFromSet:numbers intoString:NULL];
int number;
[scanner scanInt:&number];
NSString *num=[NSString stringWithFormat:@"%ld",number];
[numTmpArray addObject:num];
}
if (numTmpArray.count) {
NSRange range = NSMakeRange(0, 0);
for (int i = 0; i < numTmpArray.count; i++) {
range = [text rangeOfString:numTmpArray[i]];
[resAttributed addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:size] range:range];
}
}else{
[resAttributed addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:size] range:NSMakeRange(0, text.length)];
}
return resAttributed;
}
网友评论