本篇记录关于计算文本高度和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. ASP.NET MVC Application_Error 无效不执行

    我们一般在开发 ASP.NET MVC 应用程序的时候,会在 Application_Error 中添加异常日志记录,一般会记录 500 的错误信息,但如果应用程序在出错的时候,Application ...

  2. 1Z0-053 争议题目解析212

    1Z0-053 争议题目解析212 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 212.Note the following parameters settings in you ...

  3. 1Z0-053 争议题目解析470

    1Z0-053 争议题目解析470 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 470.Which NLS parameter can be used to change the ...

  4. Linux(Centos)快速搭建SVN

    前言 项目中源码或者文档需要进行管理与版本记录,历数此类工具VSS.CVS.SVN.GIT等等,有非常多的版本控制系统.SVN现在还是很常见,把笔记总结搬上博客,SVN这个再不放以后估计只能写GIT的 ...

  5. IntelliJ IDEA 转移C盘.IntelliJIdea(索引目录)

    转移原因: C盘是机械硬盘,并且容量不多的情况下,建议转移. 转移步骤: 找到索引目录 win10系统下默认路径:C:\Users\asus\.IntelliJIdea2016.2 *复制或剪切到新的 ...

  6. [Asp.net 5] Logging-日志系统的基本架构(上)

    本节主要介绍解决方案中的Microsoft.Framework.Logging.Abstractions.Microsoft.Framework.Logging俩个工程. 这俩个工程中所有类的关系如下 ...

  7. 初入网络系列笔记(4)HTTP请求和响应

    一.借鉴说明,本博文借鉴以下博文 1.starok,HTTP必知必会,http://www.cnblogs.com/starstone/p/4890409.html 2.CareySon,HTTP协议 ...

  8. Git分布式版本控制学习

    git和SVN都是版本控制系统.git是命令行操作,不喜欢的就算了,看完如果有身体不适还请及时就医~ git  WIN32百度网盘下载地址:http://pan.baidu.com/s/1c1AeY9 ...

  9. CSS学习

    标签选择器,标签名{},作用于所有此标签. 类选择器, .class{},在标签内定义class="",属图形结构. ID选择器,#ID{}, 在标签内定义id="&qu ...

  10. SVG图案填充-Pattern

    SVG图案一般用于SVG图形对象的填充fill或描边stroke.这个图形可以是一个SVG元素,也可以是位图图像,通过<pattern>元素在x轴或y轴方向以固定的间隔平铺. <pa ...