iOS开发之功能模块--计算高度Demo探究手稿
本篇记录关于计算文本高度和Label高度的代码,以备后期再探究:
首先是YouXianMing老师的工具类别:
NSString+LabelWidthAndHeight.h
1 // 2 // NSString+LabelWidthAndHeight.h 3 // ZiPeiYi 4 // 5 // Created by YouXianMing on 15/12/9. 6 // Copyright © 2015年 YouXianMing. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 #import <UIKit/UIKit.h> 11 12 @interface NSString (LabelWidthAndHeight) 13 14 /** 15 * Get the string's height with the fixed width. 16 * 17 * @param attribute String's attribute, eg. attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:18.f]} 18 * @param width Fixed width. 19 * 20 * @return String's height. 21 */ 22 - (CGFloat)heightWithStringAttribute:(NSDictionary <NSString *, id> *)attribute fixedWidth:(CGFloat)width; 23 24 /** 25 * Get the string's width. 26 * 27 * @param attribute String's attribute, eg. attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:18.f]} 28 * 29 * @return String's width. 30 */ 31 - (CGFloat)widthWithStringAttribute:(NSDictionary <NSString *, id> *)attribute; 32 33 /** 34 * Get a line of text height. 35 * 36 * @param attribute String's attribute, eg. attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:18.f]} 37 * 38 * @return String's width. 39 */ 40 + (CGFloat)oneLineOfTextHeightWithStringAttribute:(NSDictionary <NSString *, id> *)attribute; 41 42 43 - (CGSize)getSizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size; 44 45 @end
NSString+LabelWidthAndHeight.m
1 // 2 // NSString+LabelWidthAndHeight.m 3 // ZiPeiYi 4 // 5 // Created by YouXianMing on 15/12/9. 6 // Copyright © 2015年 YouXianMing. All rights reserved. 7 // 8 9 #import "NSString+LabelWidthAndHeight.h" 10 11 @implementation NSString (LabelWidthAndHeight) 12 13 - (CGFloat)heightWithStringAttribute:(NSDictionary <NSString *, id> *)attribute fixedWidth:(CGFloat)width { 14 15 NSParameterAssert(attribute); 16 17 CGFloat height = 0; 18 19 if (self.length) { 20 21 CGRect rect = [self boundingRectWithSize:CGSizeMake(width, MAXFLOAT) 22 options:NSStringDrawingTruncatesLastVisibleLine |NSStringDrawingUsesLineFragmentOrigin | 23 NSStringDrawingUsesFontLeading 24 attributes:attribute 25 context:nil]; 26 27 height = rect.size.height; 28 } 29 30 return height; 31 } 32 33 - (CGFloat)widthWithStringAttribute:(NSDictionary <NSString *, id> *)attribute { 34 35 NSParameterAssert(attribute); 36 37 CGFloat width = 0; 38 39 if (self.length) { 40 41 CGRect rect = [self boundingRectWithSize:CGSizeMake(MAXFLOAT, 0) 42 options:NSStringDrawingTruncatesLastVisibleLine |NSStringDrawingUsesLineFragmentOrigin | 43 NSStringDrawingUsesFontLeading 44 attributes:attribute 45 context:nil]; 46 width = rect.size.width; 47 } 48 49 return width; 50 } 51 52 + (CGFloat)oneLineOfTextHeightWithStringAttribute:(NSDictionary <NSString *, id> *)attribute { 53 54 CGFloat height = 0; 55 CGRect rect = [@"One" boundingRectWithSize:CGSizeMake(200, MAXFLOAT) 56 options:NSStringDrawingTruncatesLastVisibleLine |NSStringDrawingUsesLineFragmentOrigin | 57 NSStringDrawingUsesFontLeading 58 attributes:attribute 59 context:nil]; 60 height = rect.size.height; 61 return height; 62 } 63 64 - (CGSize)getSizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size{ 65 NSParameterAssert(font); 66 CGSize resultSize = CGSizeZero; 67 if (self.length <= 0) { 68 return resultSize; 69 } 70 resultSize = [self boundingRectWithSize:size 71 options:(NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin) 72 attributes:@{NSFontAttributeName: font} 73 context:nil].size; 74 resultSize = CGSizeMake(MIN(size.width, ceilf(resultSize.width)), MIN(size.height, ceilf(resultSize.height))); 75 return resultSize; 76 } 77 78 @end
然后是我的探究代码,见笑:
1 #import "ViewController.h" 2 #import "NSString+LabelWidthAndHeight.h" 3 4 #define Content @"字体ad大家\n好,我叫帅哥,你们\n\n\n\n\n\n字体ad大家\n好,我叫帅哥,你们都是好人你们都是好人你们都是好人你们都是好人\n都是不哦联赛结案件\n发;个;案发金额了\n国家;姐夫都\n\n\n\n\n\n是不哦联赛结案件\n发;个;案发金额了\n国家;姐夫" 5 6 7 #define LabelFont [UIFont systemFontOfSize:14] 8 @interface ViewController () 9 10 @end 11 12 @implementation ViewController 13 14 - (void)viewDidLoad { 15 [super viewDidLoad]; 16 17 // CGFloat labelHeigh = [Content heightWithStringAttribute:attDic fixedWidth:labelWidth]; 18 // NSDictionary *attDic = @{NSFontAttributeName : [UIFont systemFontOfSize:14]}; 19 CGFloat labelWidth = 200; 20 CGSize maxSize = CGSizeMake(labelWidth, MAXFLOAT); 21 CGFloat labelHeigh = [Content getSizeWithFont:LabelFont constrainedToSize:maxSize].height; 22 CGFloat aHeigh = labelHeigh; 23 24 25 26 27 // NSLog(@"计算出来的高度是:%lf",labelHeigh); 28 UILabel *label = [UILabel new]; 29 label.font = LabelFont; 30 label.frame = CGRectMake(0, 200, labelWidth , 3); 31 label.numberOfLines = 0; 32 label.lineBreakMode = NSLineBreakByCharWrapping; 33 label.text = Content; 34 label.backgroundColor = [UIColor cyanColor]; 35 36 [label sizeToFit]; 37 // NSLog(@"计算出来Label的高度是:%lf",label.frame.size.height); 38 // NSLog(@"计算出来Label的宽度是:%lf",label.frame.size.width); 39 40 label.frame = CGRectMake(0, 200, label.frame.size.width, label.frame.size.height); 41 [self.view addSubview:label]; 42 43 NSLog(@"计算高度的差值:%lf",label.frame.size.height - aHeigh); 44 45 // CGSize size = CGSizeMake(200, 3); 46 // UILabel *label = [UILabel new]; 47 // label.font = [UIFont systemFontOfSize:18]; 48 // label.numberOfLines = 0; 49 // label.lineBreakMode = NSLineBreakByCharWrapping; 50 // label.text = Content; 51 // CGRect labelFrame = label.frame; 52 // labelFrame.size = size; 53 // label.frame = labelFrame; 54 // [label sizeToFit]; 55 // labelFrame = label.frame; 56 // label.backgroundColor = [UIColor cyanColor]; 57 //// resultSize = labelFrame.size; 58 // NSLog(@"计算出来Label的高度是:%lf",label.frame.size.height); 59 // NSLog(@"计算出来Label的宽度是:%lf",label.frame.size.width); 60 // [self.view addSubview:label]; 61 62 63 UILabel *label2 = [UILabel new]; 64 label2.frame = CGRectMake(0, 0, labelWidth, aHeigh); 65 label2.font = LabelFont; 66 label2.text = Content; 67 label2.lineBreakMode = NSLineBreakByCharWrapping; 68 label2.numberOfLines = 0; 69 label2.backgroundColor = [UIColor redColor]; 70 [self.view addSubview:label2]; 71 72 NSLog(@"屏幕的宽度%lf",[UIScreen mainScreen].bounds.size.width); 73 74 75 76 } 77 78 79 @end