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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,595评论 25 709
  • Swift版本点击这里欢迎加入QQ群交流: 594119878最新更新日期:18-09-17 About A cu...
    ylgwhyh阅读 25,681评论 7 249
  • 一、好言 不要动不动就倾其所有,与其卑微到尘土里,不如留一些骄傲与疼爱给自己,最卑贱不过感情,最凉不过人心 二、背...
    吴世浩阅读 1,508评论 0 0
  • “生活不止眼前的苟且,还有诗和远方。” 今日原本计划赶八点的城铁去黄冈,带上一些月饼,去看看你。明日便是中秋节,本...
    ClaireW阅读 1,027评论 4 0
  • 数据列颜色 在colors中指定,有默认值 数据列调用颜色的方式是第n个数据列使用第n个颜色,当序列的数量超过颜色...
    我_巨可爱阅读 786评论 0 0