iOS 设置字符串小数点前后字体大小不同

为 UILabel 添加分类方法

-(void)setText:(NSString *)Text frontFont:(CGFloat)frontFont behindFont:(CGFloat)behindFont textColor:(UIColor *)textColor{
    
    //分隔字符串
    NSString *lastStr;
    NSString *firstStr;
    
    if ([Text containsString:@"."]) {
        NSRange range = [Text rangeOfString:@"."];
        lastStr = [Text substringFromIndex:range.location];
        firstStr = [Text substringToIndex:range.location];
    }

    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc] initWithString:Text];
    
    //小数点前面的字体大小
    [AttributedStr addAttribute:NSFontAttributeName
                          value:[UIFont boldSystemFontOfSize:frontFont]
                          range:NSMakeRange(0, firstStr.length)];
    
    //小数点后面的字体大小
    [AttributedStr addAttribute:NSFontAttributeName
                          value:[UIFont boldSystemFontOfSize:behindFont]
                          range:NSMakeRange(firstStr.length, lastStr.length)];
    //字符串的颜色
    [AttributedStr addAttribute:NSForegroundColorAttributeName
                          value:textColor
                          range:NSMakeRange(0, Text.length)];
    
    self.attributedText = AttributedStr;
}

你可能感兴趣的:(iOS 设置字符串小数点前后字体大小不同)