8.根据指定文本,字体,固定宽度或高度,求尺寸
// 根据指定文本,字体和大高度计算尺寸
- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxHeight:(CGFloat)height
{
NSMutableDictionary *attrDict = [NSMutableDictionary dictionary];
attrDict[NSFontAttributeName] = font;
CGSize size = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, height) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrDict context:nil].size;
return size;
}
// 根据指定文本,字体和大宽度计算尺寸
- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxWidth:(CGFloat)width
{
NSMutableDictionary *attrDict = [NSMutableDictionary dictionary];
attrDict[NSFontAttributeName] = font;
CGSize size = [text boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrDict context:nil].size;
return size;
}
  9.给UILabel设置行间距,字间距等富文本属性,并计算高度
//给UILabel设置行间距和字间距
-(void)setLabelSpace:(UILabel*)label withValue:(NSString*)str withFont:(UIFont*)font {
NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
paraStyle.lineBreakMode = NSLineBreakByCharWrapping; //结尾部分的文字省略方式
paraStyle.alignment = NSTextAlignmentLeft; //文本对齐方式
paraStyle.lineSpacing = 7; //设置行间距
paraStyle.hyphenationFactor = 1.0; //连字属性 在iOS,支持的值分别为0和1
paraStyle.firstLineHeadIndent = 0.0; //首行缩进
paraStyle.paragraphSpacingBefore = 0.0; //段首行空白空间
paraStyle.headIndent = 0; //整体缩进(首行除外)
paraStyle.paragraphSpacing = 15; //段与段之间的间距
//设置字间距 NSKernAttributeName:@1.5f
NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f};
NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:str attributes:dic];
label.attributedText = attributeStr;
}
//计算UILabel的高度(带有行间距的情况)
-(CGFloat)getSpaceLabelHeight:(NSString*)str withFont:(UIFont*)font withWidth:(CGFloat)width {
NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
paraStyle.alignment = NSTextAlignmentLeft;
paraStyle.lineSpacing = 7;
paraStyle.hyphenationFactor = 1.0;
paraStyle.firstLineHeadIndent = 0.0;
paraStyle.paragraphSpacingBefore = 0.0;
paraStyle.headIndent = 0;
paraStyle.paragraphSpacing = 15;
NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f};
CGSize size = [str boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
return size.height;
}