计算文字高度和宽度

//计算lab高度

- (CGFloat)getSizeForLabString:(NSString *)labStr font:(NSInteger)fontInt width:(CGFloat)width

{

    NSMutableAttributedString *attrStr = [[NSMutableAttributedStringalloc]initWithString:labStr];

    

    NSRange allRange = [labStrrangeOfString:labStr];

    [attrStr addAttribute:NSFontAttributeName

                    value:[UIFontsystemFontOfSize:KHEIGHTShiPei fontInt]

                    range:allRange];

    

    CGFloat titleHeight;

    NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading;

    CGRect rect = [attrStrboundingRectWithSize:CGSizeMake(width,CGFLOAT_MAX)

                                        options:options

                                        context:nil];

    titleHeight = ceilf(rect.size.height);

    return titleHeight+2//加两个像素,防止emoji被切掉.

}


//计算lab宽度

- (CGFloat)getSizeForLabString:(NSString *)labStr font:(NSInteger)fontInt hight:(CGFloat)hight

{

    if (labStr ==nil) {

        return0;

    }

    NSMutableAttributedString *attrStr = [[NSMutableAttributedStringalloc]initWithString:labStr];

    

    NSRange allRange = [labStrrangeOfString:labStr];

    [attrStr addAttribute:NSFontAttributeName

                    value:[UIFontsystemFontOfSize:KHEIGHTShiPei fontInt]

                    range:allRange];

    

    CGFloat titleWidth;

    NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading;

    CGRect rect = [attrStrboundingRectWithSize:CGSizeMake(CGFLOAT_MAX, hight)

                                        options:options

                                        context:nil];

    titleWidth = ceilf(rect.size.width);

    return titleWidth+2;

}

iPhone 透明手机壳+全包手机膜+home键透明贴+摄像头保护膜 = 9.9元

https://item.taobao.com/item.htm?spm=a1z38n.10677092.0.0.4782665ZUrF2Q&id=562834644439

计算文字高度和宽度_第1张图片计算文字高度和宽度_第2张图片计算文字高度和宽度_第3张图片

你可能感兴趣的:(常用方法)