BBAFitSizeLabel.h 3.45 KB
//
//  BBAFitSizeLabel.h
//  fontHeight
//
//  Created by ZhangJian on 16/9/1.
//  Copyright © 2016年 zhangjian. All rights reserved.
//

#import <UIKit/UIKit.h>

extern NSString * fontSizeKey;
extern NSString * textColorKey;
extern NSString * textKey;
extern NSString * imageKey;

/**
 *鉴于系统NSAttributedString常常会有一个莫名其妙的边儿,也就是使用boundingRectWithSize计算出来的高度常常有问题在布局里面,包括行与直接的间隔跟实际的像素也是不一样的。所以这里创建了一个类来实现精确的字符串的布局。
 *This class is used to calculate the NSAttributedString precisely.
 */
@interface BBAFitSizeLabel : UILabel


/**
 * 设置获取取得计算文字真正的到顶部的距离 或者frame等
 */
@property(nonatomic, assign)CGRect pureTextFrame;  //逻辑Frame


/**
 * 如果你要设置该类的属性,也就是说如果要设置attributedText,必须使用下面的方法生成attributedString你一定要使用下面的方法,而不是自己去创建一个。
 * If you want to set attributedString of this, you should use the function below, but not create one attributeString by yourself.
 */
-(NSAttributedString *)makeAnAttributedString:(NSString *)text andLineSpace:(CGFloat)lineSpace andFont:(UIFont*)font andBreakMode:(NSLineBreakMode)lineMode andTextColor:(UIColor *)textColor andWidth:(CGFloat)width andMaxNumberOfLines:(int)lineNumber;

/***
 * 计算Size的时候必须使用下面的方法。
 * This function is Used To calculate the size of one string.
 */
+(CGSize)sizeThatFit:(NSString *)text andFont:(UIFont *)font andLineSpace:(CGFloat)lineSpace andMaxLinesNumber:(int)lineNumber andFitSize:(CGSize)size;



//////////////////////////////////////////////////////////////////////////////////////////////////////////////


+(CGSize)sizeThatFit:(NSArray *)textDicArray andLineSpace:(CGFloat)lineSpace andMaxLinesNumber:(int)lineNumber andFitSize:(CGSize)size;

/**
 *textDicArray 是一个由字典组成的数组。
 *key分别是
 * fontSizeKey(非必须参数);
 * textColorKey(非必须参数);
 * lineSpaceKey(非必须参数);
 * textKey (必需参数);
 * imageKey (必需参数);
 */
-(NSAttributedString *)makeAnAttributedString:(NSArray *)textDicArray  andLinespace:(CGFloat)linespacet;





// 生成 AttributedString
+ (NSAttributedString *)getAttributedString:(NSString *)text textFont:(UIFont *)font lineSpace:(CGFloat)space;
+ (NSAttributedString *)getAttributedString:(NSString *)text textFont:(UIFont *)font lineSpace:(CGFloat)space lineBreakMode:(NSLineBreakMode)lineBreakMode;
+ (NSAttributedString *)getAttributedString:(NSString *)text textFont:(UIFont *)font lineSpace:(CGFloat)space lineBreakMode:(NSLineBreakMode)lineBreakMode textAlignment:(NSTextAlignment)textAlignment;
+ (NSAttributedString *)getAttributedString:(NSString *)text textFont:(UIFont *)font lineSpace:(CGFloat)space lineBreakMode:(NSLineBreakMode)lineBreakMode textAlignment:(NSTextAlignment)textAlignment textColor:(UIColor *)textColor;

/**
 *
 * @brief 返回文本行高
 *
 */
+ (CGFloat)heightForContent:(NSString *)content lineWidth:(CGFloat)width font:(UIFont *)font maxLines:(NSUInteger)lineCount lineSpaces:(CGFloat)space;

/**
 *
 * @brief 判断是否为单行文本
 *
 */
+ (BOOL)isOneLineText:(NSString *)content font:(UIFont *)font space:(CGFloat)space width:(CGFloat)width;

/**
 *
 * @brief 单行高度
 *
 */
+ (CGFloat)heightForOneLineByFont:(UIFont *)font lineSpace:(CGFloat)space;

@end