本篇记录关于计算文本高度和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

iOS开发之功能模块--计算高度Demo探究手稿的更多相关文章

  1. iOS开发之功能模块--高仿Boss直聘的常用语的开发

    首先上Boss直聘的功能界面截图,至于交互请读者现在Boss直聘去交互体验:     本人的公司项目要高仿Boss直聘的IM常用语的交互功能,居然花费了我前后17个小时完成,这回自己测试了很多遍,代码 ...

  2. IOS开发之功能模块--自定义导航控制器类常用自定义的代码

    前言:本文篇幅不多,但是涉及到的内容却是开发中常用的. 涉及的内容: 1.统一设置导航控制器子控制器的返回按钮. 2.因为修改了系统的返回按钮,所以还需要设置手势事件. 3.隐藏底部的工具条. 这里直 ...

  3. iOS开发之功能模块--Apns推送中的的json格式介绍

    在开发向苹果Apns推送消息服务功能,我们需要根据Apns接受的数据格式进行推送.下面接受我在进行apns推送时候总结的一点apns服务接受的Json数据格式 示例 1: 以下负载包含哦一个简单的 a ...

  4. IOS开发之功能模块--给任意的UIView添加点击事件

    前言:好久没写博客,今天来一波.我在实际项目开发中,会遇到这样功能需求:我已经搭好了iOS的UI界面,但是很多界面的子View用了UIView,然后这些UIView中用了UILabel和UIImage ...

  5. iOS开发之功能模块--根据需求开发横向的子弹盒View

    这个需求是本人工作开发中后期需求要添加的新功能,本人模仿UITableView的代理和数据源方法进行了第一阶段的开发.第二阶段是添加丰富的动画. 这个功能需求描述:能上传添加五个待选头像,五个头像分别 ...

  6. iOS开发之功能模块--推送之坑问题解决

    不管想不想看我后面再开发中总结的经验,但是很值得推荐一位大神写的关于苹果推送,很多内容哦:http://www.cnblogs.com/qiqibo/category/408304.html 苹果开发 ...

  7. iOS开发之功能模块--高仿Boss直聘的IM界面交互功能

    本人公司项目属于社交类,高仿Boss直聘早期的版本,现在Boss直聘界面风格,交互风格都不如Boss直聘以前版本的好看. 本人通过iPhone模拟器和本人真机对聊,将完成的交互功能通过Mac截屏模拟器 ...

  8. iOS开发之功能模块--本地序列化

    下面只展示项目开发中,本地序列化的示例代码: AuthenticationManager.h #import <Foundation/Foundation.h> #import " ...

  9. IOS开发之功能模块--输入框随着键盘的位置移动而移动

    废话不多说,先直接上效果图: 先熟悉一下在Cocoa框架中会用到的key键: 然后直接上Demo的源码截图: 看代码之前,补充说一句,Demo中的文本框以及文本框的背后灰色的View是通过storyb ...

随机推荐

  1. Oracle_SQL函数-单行函数

    SQL函数 SQL函数分类 SQL函数主要有两种,分为单行函数.多行函数 单行函数:只对一行进行变换,每行返回一个结果.可以转换数据类型,可以嵌套参数可以是一列或一个值 多行函数:多行函数,每次对一组 ...

  2. OracleDBA之数据库管理

    以下这些东西是我的麦库上存的当时学Oracle的学习笔记今天拿出来和大家分享一下,转载请注明出处,下面用的Oracle的版本是10g,用的时WinServer2003的操作系统,可能有些命令和Orac ...

  3. ReactJS分析之入口函数render

    前言 在使用React进行构建应用时,我们总会有一个步骤将组建或者虚拟DOM元素渲染到真实的DOM上,将任务交给浏览器,进而进行layout和paint等步骤,这个函数就是React.render() ...

  4. struts2学习笔记--动手搭建环境+第一个helloworld项目

    在Myeclipse中已经内置好了struts2的环境,但是为了更好的理解,这里自己从头搭建一下: 前期准备:下载struts2的完整包,下载地址:https://struts.apache.org/ ...

  5. linker command failed with exit code

    转载 : http://blog.csdn.net/chengwuli125/article/details/25051741

  6. JS控制的事件

    鼠标点击弹出提示事件: <input type="button" value="鼠标点击弹出提示" onclick="DianJi()" ...

  7. composer安装yii2问题总结

    今天周六,在家安装yii2的advanced版本, 过程有些坎坷, 不过最后总算安装好了. 总结一下, 主要遇到下面两个问题: 1, 下载速度慢, 主要原因是网络问题 下载yii2时, 模板(除了ve ...

  8. TCP滑动窗口机制的简洁模型

    模型图: 我们可以大概看一下上图的模型: 首先是AB之间三次握手建立TCP连接.在报文的交互过程中,A将自己的缓冲区大小(窗口大小)3发送给B,B同理,这样双方就知道了对端的窗口大小. A开始发送数据 ...

  9. ES6笔记系列

    ES6,即ECMAScript 6.0,ES6的第一个版本是在2015年发布的,所以又称作ECMAScript 2015 如今距ES6发布已经一年多的时间,这时候才去学,作为一个JSer,羞愧羞愧,还 ...

  10. ASP.NET Core 开发-中间件(Middleware)

    ASP.NET Core开发,开发并使用中间件(Middleware). 中间件是被组装成一个应用程序管道来处理请求和响应的软件组件. 每个组件选择是否传递给管道中的下一个组件的请求,并能之前和下一组 ...