本篇记录关于计算文本高度和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. tn文本分析语言(四) 实现自然语言计算器

    tn是desert和tan共同开发的一种用于匹配,转写和抽取文本的语言.解释器使用Python实现,代码不超过1000行. github地址:https://github.com/ferventdes ...

  2. Oracle 11g安装GI后,运行roothas.pl脚本报错libcap.so.1找不到

    环境:RHEL6.4 + Oracle 11.2.0.3问题:需求是文件系统迁移到ASM,在安装GI后,运行roothas.pl脚本报错 1.运行root.sh后,按提示运行roothas.pl报错 ...

  3. SQL SERVER 通用分页存储过程,两种用法任你选

    写在前面 从SQLSERVER 2005开始,提供了Row_Number()函数,利用函数生成的Index来处理分页,按照正常的逻辑思维都是传pageIndex和pageSize来完成分页,昨天前端和 ...

  4. 【JVM】JVM系列之垃圾回收(二)

    一.为什么需要垃圾回收 如果不进行垃圾回收,内存迟早都会被消耗空,因为我们在不断的分配内存空间而不进行回收.除非内存无限大,我们可以任性的分配而不回收,但是事实并非如此.所以,垃圾回收是必须的. 二. ...

  5. .NET开源高性能Socket通信中间件Helios介绍及演示

    一:Helios是什么 Helios是一套高性能的Socket通信中间件,使用C#编写.Helios的开发受到Netty的启发,使用非阻塞的事件驱动模型架构来实现高并发高吞吐量.Helios为我们大大 ...

  6. FreeRTOS 中断优先级嵌套错误引发HardFault异常解决(转)

      最近在使用FreeRTOS的时候,突然发现程序在运行了几分钟之后所有的任务都不再调用了,只有几个中断能正常使用,看来是系统挂掉了,连续测试了几次想找出问题,可是这个真的有点不知所措.      我 ...

  7. 解决asp.net mvc的跨域请求问题

    web.config中配置如下内容: <system.webServer> <httpProtocol> <customHeaders> <add name= ...

  8. iOS学习笔记——AutoLayout的约束

    iOS学习笔记——AutoLayout约束 之前在开发iOS app时一直以为苹果的布局是绝对布局,在IB中拖拉控件运行或者直接使用代码去调整控件都会发上一些不尽人意的结果,后来发现iOS在引入了Au ...

  9. CSS3透明属性opacity

    例子: <div id="fixhovertree" style="position:fixed;left:100px;width:120px;top:100px; ...

  10. HTML中嵌入pdf的简单方法

    <embed src="> 或者你不想显示某些功能的话: <embed src=">