iOS开发之功能模块--计算高度Demo探究手稿

本篇记录关于计算文本高度和Label高度的代码,以备后期再探究:

首先是YouXianMing老师的工具类别:

NSString+LabelWidthAndHeight.h

 //
// NSString+LabelWidthAndHeight.h
// ZiPeiYi
//
// Created by YouXianMing on 15/12/9.
// Copyright © 2015年 YouXianMing. All rights reserved.
// #import <Foundation/Foundation.h>
#import <UIKit/UIKit.h> @interface NSString (LabelWidthAndHeight) /**
* Get the string's height with the fixed width.
*
* @param attribute String's attribute, eg. attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:18.f]}
* @param width Fixed width.
*
* @return String's height.
*/
- (CGFloat)heightWithStringAttribute:(NSDictionary <NSString *, id> *)attribute fixedWidth:(CGFloat)width; /**
* Get the string's width.
*
* @param attribute String's attribute, eg. attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:18.f]}
*
* @return String's width.
*/
- (CGFloat)widthWithStringAttribute:(NSDictionary <NSString *, id> *)attribute; /**
* Get a line of text height.
*
* @param attribute String's attribute, eg. attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:18.f]}
*
* @return String's width.
*/
+ (CGFloat)oneLineOfTextHeightWithStringAttribute:(NSDictionary <NSString *, id> *)attribute; - (CGSize)getSizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size; @end

NSString+LabelWidthAndHeight.m

 //
// NSString+LabelWidthAndHeight.m
// ZiPeiYi
//
// Created by YouXianMing on 15/12/9.
// Copyright © 2015年 YouXianMing. All rights reserved.
// #import "NSString+LabelWidthAndHeight.h" @implementation NSString (LabelWidthAndHeight) - (CGFloat)heightWithStringAttribute:(NSDictionary <NSString *, id> *)attribute fixedWidth:(CGFloat)width { NSParameterAssert(attribute); CGFloat height = ; if (self.length) { CGRect rect = [self boundingRectWithSize:CGSizeMake(width, MAXFLOAT)
options:NSStringDrawingTruncatesLastVisibleLine |NSStringDrawingUsesLineFragmentOrigin |
NSStringDrawingUsesFontLeading
attributes:attribute
context:nil]; height = rect.size.height;
} return height;
} - (CGFloat)widthWithStringAttribute:(NSDictionary <NSString *, id> *)attribute { NSParameterAssert(attribute); CGFloat width = ; if (self.length) { CGRect rect = [self boundingRectWithSize:CGSizeMake(MAXFLOAT, )
options:NSStringDrawingTruncatesLastVisibleLine |NSStringDrawingUsesLineFragmentOrigin |
NSStringDrawingUsesFontLeading
attributes:attribute
context:nil];
width = rect.size.width;
} return width;
} + (CGFloat)oneLineOfTextHeightWithStringAttribute:(NSDictionary <NSString *, id> *)attribute { CGFloat height = ;
CGRect rect = [@"One" boundingRectWithSize:CGSizeMake(, MAXFLOAT)
options:NSStringDrawingTruncatesLastVisibleLine |NSStringDrawingUsesLineFragmentOrigin |
NSStringDrawingUsesFontLeading
attributes:attribute
context:nil];
height = rect.size.height;
return height;
} - (CGSize)getSizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size{
NSParameterAssert(font);
CGSize resultSize = CGSizeZero;
if (self.length <= ) {
return resultSize;
}
resultSize = [self boundingRectWithSize:size
options:(NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin)
attributes:@{NSFontAttributeName: font}
context:nil].size;
resultSize = CGSizeMake(MIN(size.width, ceilf(resultSize.width)), MIN(size.height, ceilf(resultSize.height)));
return resultSize;
} @end

然后是我的探究代码,见笑:

 #import "ViewController.h"
#import "NSString+LabelWidthAndHeight.h" #define Content @"字体ad大家\n好,我叫帅哥,你们\n\n\n\n\n\n字体ad大家\n好,我叫帅哥,你们都是好人你们都是好人你们都是好人你们都是好人\n都是不哦联赛结案件\n发;个;案发金额了\n国家;姐夫都\n\n\n\n\n\n是不哦联赛结案件\n发;个;案发金额了\n国家;姐夫" #define LabelFont [UIFont systemFontOfSize:14]
@interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; // CGFloat labelHeigh = [Content heightWithStringAttribute:attDic fixedWidth:labelWidth];
// NSDictionary *attDic = @{NSFontAttributeName : [UIFont systemFontOfSize:14]};
CGFloat labelWidth = ;
CGSize maxSize = CGSizeMake(labelWidth, MAXFLOAT);
CGFloat labelHeigh = [Content getSizeWithFont:LabelFont constrainedToSize:maxSize].height;
CGFloat aHeigh = labelHeigh; // NSLog(@"计算出来的高度是:%lf",labelHeigh);
UILabel *label = [UILabel new];
label.font = LabelFont;
label.frame = CGRectMake(, , labelWidth , );
label.numberOfLines = ;
label.lineBreakMode = NSLineBreakByCharWrapping;
label.text = Content;
label.backgroundColor = [UIColor cyanColor]; [label sizeToFit];
// NSLog(@"计算出来Label的高度是:%lf",label.frame.size.height);
// NSLog(@"计算出来Label的宽度是:%lf",label.frame.size.width); label.frame = CGRectMake(, , label.frame.size.width, label.frame.size.height);
[self.view addSubview:label]; NSLog(@"计算高度的差值:%lf",label.frame.size.height - aHeigh); // CGSize size = CGSizeMake(200, 3);
// UILabel *label = [UILabel new];
// label.font = [UIFont systemFontOfSize:18];
// label.numberOfLines = 0;
// label.lineBreakMode = NSLineBreakByCharWrapping;
// label.text = Content;
// CGRect labelFrame = label.frame;
// labelFrame.size = size;
// label.frame = labelFrame;
// [label sizeToFit];
// labelFrame = label.frame;
// label.backgroundColor = [UIColor cyanColor];
//// resultSize = labelFrame.size;
// NSLog(@"计算出来Label的高度是:%lf",label.frame.size.height);
// NSLog(@"计算出来Label的宽度是:%lf",label.frame.size.width);
// [self.view addSubview:label]; UILabel *label2 = [UILabel new];
label2.frame = CGRectMake(, , labelWidth, aHeigh);
label2.font = LabelFont;
label2.text = Content;
label2.lineBreakMode = NSLineBreakByCharWrapping;
label2.numberOfLines = ;
label2.backgroundColor = [UIColor redColor];
[self.view addSubview:label2]; NSLog(@"屏幕的宽度%lf",[UIScreen mainScreen].bounds.size.width); } @end
上一篇:JavaWeb-11 (JSP&EL表达)


下一篇:JavaWeb---总结(十)JSP标签