开发金融app,经常遇到一个简单Label中字体大小颜色不一致的情况,如下图
下面的代码就是简单的把万元、月的字体变小,颜色改变成自己想要的效果。NSString的类目
#import "NSString+AttributeString.h"
@implementation NSString (AttributeString)
+(NSMutableAttributedString *)attributedStringFromString:(NSString *)string withLength:(CGFloat)length withFont:(CGFloat)font withColor:(UIColor *)color{
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString: string];
[attributeString addAttributes: @{NSFontAttributeName:[UIFont systemFontOfSize:font],NSForegroundColorAttributeName: color}range: NSMakeRange(string.length-length, length)];
return attributeString;
}
@end
使用方法:
UIColor *orangeColor = [UIColor colorWithHexString:MainOrangeColor];
[allCountLabel setAttributedText:[NSString attributedStringFromString:[NSString stringWithFormat:@"%@万",_model.money] withLength:1 withFont:18 withColor:orangeColor]];
更多源码请访问github:https://github.com/zhangjiahuan8888