//
//  NSString+XVAdd.m
//  fastservice
//
//  Created by xuning on 2/3/24.
//  Copyright © 2024 FastService. All rights reserved.
//

#import "NSString+XVAdd.h"

@implementation NSString (XVAdd)



- (CGFloat)heightForString:(NSString *)text font:(UIFont *)font width:(CGFloat)width {
    
    NSDictionary *attributes = @{NSFontAttributeName : font};
    CGSize sizeToFit = [text boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin  attributes:attributes context:nil].size;
    return sizeToFit.height;
}

/**
 *  计算当前字符串的size
 *
 *  @param font    使用的字体
 *  @param maxSize 最大的size
 *
 *  @return 需要的最小size
 */
- (CGSize)sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize
{
    CGSize size = CGSizeZero;
    if ([self respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) {
        size = [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : font} context:nil].size;
    }
    //额外增加一个点，防止计算误差
    return CGSizeMake(size.width + 1, size.height);
}
/**
 *  计算富文本字体高度
 *
 *  @param lineSpeace 行高
 *  @param font       字体
 *  @param width      字体所占宽度
 *
 *  @return 富文本高度
 */
-(CGFloat)getSpaceLabelHeightwithSpeace:(CGFloat)lineSpeace withFont:(UIFont*)font withWidth:(CGFloat)width {
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    //    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
    /** 行高 */
    paraStyle.lineSpacing = lineSpeace;
    // NSKernAttributeName字体间距
    NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle};
    CGSize size = [self boundingRectWithSize:CGSizeMake(width,MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
    return size.height;
}



@end
