用户登录
用户注册

分享至

设置字符串中的数字的颜色和字体

  • 作者: 淡忘NB
  • 来源: 51数据库
  • 2022-08-17
+(void)setRichNumberWithLabel:(UILabel*)label Color:(UIColor *) color FontSize:(CGFloat)size
{
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:label.text];
    NSString *temp = nil;
    for(int i =0; i < [attributedString length]; i++)
    {
        temp = [label.text substringWithRange:NSMakeRange(i, 1)];
        if( [self isInt:temp]  ){
            [attributedString setAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                             color, NSForegroundColorAttributeName,
                                             [UIFont systemFontOfSize:size],NSFontAttributeName, nil]
                                      range:NSMakeRange(i, 1)];
        }
    }
    
    label.attributedText = attributedString;
}
软件
前端设计
程序设计
Java相关