iOS Label中前后文字大小颜色不一致

开发金融app,经常遇到一个简单Label中字体大小颜色不一致的情况,如下图


效果图.png

下面的代码就是简单的把万元、月的字体变小,颜色改变成自己想要的效果。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

你可能感兴趣的:(iOS Label中前后文字大小颜色不一致)