字符串区分颜色和大小
2021-01-12 本文已影响0人
ChenL
image.png
iOS利用NSAttributeString实现不同颜色大小显示的方法
一个UILabel显示不同大小颜色的字符串,当然我们首先的想到属性字符串,但是注意:我们这里要处理国际化完成的字符串
比如:2分14秒 或者 2min 14secs
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *label;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//调用
NSAttributedString *resultTime = [self formattedCurrentTime:133];
self.label.attributedText = resultTime;
}
一、字符串range匹配法
/**
返回当前时间格式
@return 返回组装好的字符串
*/
- (NSAttributedString *)formattedCurrentTime:(NSTimeInterval)timeInterval {
NSUInteger time = (NSUInteger)timeInterval;
NSInteger minutes = (time / 60) % 60;
NSInteger seconds = time % 60;
NSString *minStr = [NSString stringWithFormat:@" %zd ",minutes];
NSString *secStr = [NSString stringWithFormat:@" %zd ",seconds];
//假设这就是我们国际化后的字符串
NSString *localizedFormatString = [NSString stringWithFormat:@"%@分%@秒",minStr,secStr];
NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:localizedFormatString];
NSRange minRange, secRange;
if (@available(iOS 9.0, *)) {
minRange = [localizedFormatString localizedStandardRangeOfString:minStr];
secRange = [localizedFormatString localizedStandardRangeOfString:secStr];
} else {
minRange = [localizedFormatString rangeOfString:minStr];
secRange = [localizedFormatString rangeOfString:secStr];
}
NSDictionary *timeAttrs = @{ NSForegroundColorAttributeName : [UIColor redColor],
NSFontAttributeName : [UIFont systemFontOfSize:40.0f]};
[attributeStr addAttributes:timeAttrs range:minRange];
[attributeStr addAttributes:timeAttrs range:secRange];
return [[NSAttributedString alloc] initWithAttributedString:attributeStr];;
}
二、用正则匹配数字
/**
返回当前时间格式
@return 返回组装好的字符串
*/
- (NSAttributedString *)formattedCurrentTime:(NSTimeInterval)timeInterval {
NSUInteger time = (NSUInteger)timeInterval;
NSInteger minutes = (time / 60) % 60;
NSInteger seconds = time % 60;
NSString *minStr = [NSString stringWithFormat:@" %zd ",minutes];
NSString *secStr = [NSString stringWithFormat:@" %zd ",seconds];
//假设这就是我们国际化后的字符串
NSString *localizedFormatString = [NSString stringWithFormat:@"%@分%@秒",minStr,secStr];
NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:localizedFormatString];
NSDictionary *timeAttrs = @{ NSForegroundColorAttributeName : [UIColor redColor],
NSFontAttributeName : [UIFont systemFontOfSize:40.0f]};
/** 方案2 **/
NSError *error = nil;
NSRegularExpression *reg = [NSRegularExpression regularExpressionWithPattern:@"[0-9]+" options:NSRegularExpressionCaseInsensitive error:&error];
if (error == nil) {
NSArray *matches = [reg matchesInString:localizedFormatString options:NSMatchingReportCompletion range:NSMakeRange(0, localizedFormatString.length)];
for (NSTextCheckingResult *match in matches) {
for (NSUInteger i = 0; i < match.numberOfRanges; i++) {
NSRange range = [match rangeAtIndex:i];
if (range.location != NSNotFound) {
[attributeStr addAttributes:timeAttrs range:range];
}
}
}
}
return [[NSAttributedString alloc] initWithAttributedString:attributeStr];;
}
时间格式化代码如下:
/**
返回时间格式 HH:mm:ss
@return 返回组装好的字符串
*/
- (NSString *)formattedCurrentTime {
NSUInteger time = (NSUInteger)self.recorder.currentTime;
NSInteger hours = (time / 3600);
NSInteger minutes = (time / 60) % 60;
NSInteger seconds = time % 60;
NSString *format = @"%02i:%02i:%02i";
return [NSString stringWithFormat:format, hours, minutes, seconds];
}