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
iOS开发之功能模块--计算高度Demo探究手稿的更多相关文章
- iOS开发之功能模块--高仿Boss直聘的常用语的开发
首先上Boss直聘的功能界面截图,至于交互请读者现在Boss直聘去交互体验: 本人的公司项目要高仿Boss直聘的IM常用语的交互功能,居然花费了我前后17个小时完成,这回自己测试了很多遍,代码 ...
- IOS开发之功能模块--自定义导航控制器类常用自定义的代码
前言:本文篇幅不多,但是涉及到的内容却是开发中常用的. 涉及的内容: 1.统一设置导航控制器子控制器的返回按钮. 2.因为修改了系统的返回按钮,所以还需要设置手势事件. 3.隐藏底部的工具条. 这里直 ...
- iOS开发之功能模块--Apns推送中的的json格式介绍
在开发向苹果Apns推送消息服务功能,我们需要根据Apns接受的数据格式进行推送.下面接受我在进行apns推送时候总结的一点apns服务接受的Json数据格式 示例 1: 以下负载包含哦一个简单的 a ...
- IOS开发之功能模块--给任意的UIView添加点击事件
前言:好久没写博客,今天来一波.我在实际项目开发中,会遇到这样功能需求:我已经搭好了iOS的UI界面,但是很多界面的子View用了UIView,然后这些UIView中用了UILabel和UIImage ...
- iOS开发之功能模块--根据需求开发横向的子弹盒View
这个需求是本人工作开发中后期需求要添加的新功能,本人模仿UITableView的代理和数据源方法进行了第一阶段的开发.第二阶段是添加丰富的动画. 这个功能需求描述:能上传添加五个待选头像,五个头像分别 ...
- iOS开发之功能模块--推送之坑问题解决
不管想不想看我后面再开发中总结的经验,但是很值得推荐一位大神写的关于苹果推送,很多内容哦:http://www.cnblogs.com/qiqibo/category/408304.html 苹果开发 ...
- iOS开发之功能模块--高仿Boss直聘的IM界面交互功能
本人公司项目属于社交类,高仿Boss直聘早期的版本,现在Boss直聘界面风格,交互风格都不如Boss直聘以前版本的好看. 本人通过iPhone模拟器和本人真机对聊,将完成的交互功能通过Mac截屏模拟器 ...
- iOS开发之功能模块--本地序列化
下面只展示项目开发中,本地序列化的示例代码: AuthenticationManager.h #import <Foundation/Foundation.h> #import " ...
- IOS开发之功能模块--输入框随着键盘的位置移动而移动
废话不多说,先直接上效果图: 先熟悉一下在Cocoa框架中会用到的key键: 然后直接上Demo的源码截图: 看代码之前,补充说一句,Demo中的文本框以及文本框的背后灰色的View是通过storyb ...
随机推荐
- 精彩 .NET 2015
英文原文:Understanding .NET 2015 Understanding 翻译为了解或理解,对于 .NET 来说,2015 年注定会更加精彩,所以标题就用了"精彩"这个 ...
- IDDD 实现领域驱动设计-由贫血导致的失忆症
啰嗦几句 年前的时候,在和 netfocus 兄,以及对 DDD 感兴趣园友的探讨过程中,我发现自己有很多不足的地方,对 DDD 的了解也只是皮毛而已,代码写的少,DDD 的基本概念也不是很清楚,空有 ...
- 【记录】WCF IIS 404
WCF IIS 发布报"404错误": 修改 Web.config 如下: <system.webServer> <handlers> <remove ...
- C++ 与 php 的交互 之----- C++ 异步获取 网页文字内容,异步获取 php 的 echo 值。
已搬迁至 http://www.cnblogs.com/linguanh/p/4543836.html
- 尽量使用translate而不是改变top/left进行动画(翻译)
前言 本文翻译自 Why Moving Elements With Translate() Is Better Than Pos:abs Top/left,本文有改动,添加了一些作者自己的理解,不当之 ...
- 相克军_Oracle体系_随堂笔记014-锁 latch,lock
1.Oracle锁类型 2.行级锁:DML语句 3.表级锁:TM 4.锁的兼容性 5.加锁语句以及锁的释放 6.锁相关视图 7.死锁 1.Oracle锁类型 锁的作用 latch锁:chain ...
- PHP运行及语句
php开发网页需要存放在wamp根目录下的www文件夹中才可运行成功.同时wamp要处于运行状态. 无站点情况下打开方式: 网址栏中输入:localhost/文件名称 代码规范: 用<?php ...
- CSS魔法堂:重拾Border之——不仅仅是圆角
前言 当CSS3推出border-radius属性时我们是那么欣喜若狂啊,一想到终于不用再添加额外元素来模拟圆角了,但发现border-radius还分水平半径和垂直半径,然后又发现border-t ...
- 一句jQuery代码返回顶部
一句jQuery代码返回顶部 效果体验: http://hovertree.com/texiao/yestop/ 使用方法: 只需引用jQuery库和YesTop插件,然后一句代码就可以实现返回顶部: ...
- identity与ASP.NET 模拟
默认情况下,ASP.NET应用程序以本机的ASPNET帐号运行,该帐号属于普通用户组,权限受到一定的限制,以保障ASP.NET应用程序运行的安全.但是有时需要某个ASP.NET应用程序或者程序中的某段 ...