计算字符长度

#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>

@interface CustomSize : NSObject


+ (CGSize)returnSize:(NSString *)str font:(UIFont *)font;

+ (CGSize)returnSize:(NSString *)str fontSize:(CGFloat)fontSize;

+ (CGFloat)adaptionHeight:(NSString *)str

                          size:(CGFloat)number

                         width:(CGFloat)width;


+ (CGFloat)heightForText:(NSString *)text andFontSize:(UIFont *)font;

@end

#import "CustomSize.h"


@implementation CustomSize


#pragma mark---获取字符串的长度

+ (CGSize)returnSize:(NSString *)str fontSize:(CGFloat)fontSize{

    UIFont *font = [UIFont systemFontOfSize:fontSize];

    return [CustomSize returnSize:str font:font];

}


+ (CGSize)returnSize:(NSString *)str font:(UIFont *)font{

    

    CGSize size = [str sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName, nil]];

    return size;

}

//根据文字大小自动生成height

#pragma mark---根据文字大小自动生成height

+ (CGFloat)adaptionHeight:(NSString *)str

                          size:(CGFloat)number

                         width:(CGFloat)width{

    NSDictionary *infoTextDic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:number],NSFontAttributeName, nil];

    CGRect infoTextRect = [str boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine attributes:infoTextDic context:nil];

    CGFloat heingt = infoTextRect.size.height;

    return heingt;

}

+ (CGFloat)heightForText:(NSString *)text andFontSize:(UIFont *)font

{

    //设置计算文本时字体的大小,以什么标准来计算

    NSDictionary *attrbute = @{NSFontAttributeName:font};

    return [text boundingRectWithSize:CGSizeMake(ScreenWidth, 1000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attrbute context:nil].size.height;

}


@end



阅读更多
个人分类: IOS-项目汇总
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

计算字符长度

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭