计算一行文本的高度

计算一行文本的高度

 

说明

有时候我们需要知道指定的几行文本的高度,此工具用于解决此种问题。

 

源码

//
//  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)aLineOfTextHeightWithStringAttribute:(NSDictionary <NSString *, id> *)attribute;@end
//
//  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 = 0;if (self.length) {CGRect rect = [self boundingRectWithSize:CGSizeMake(width, MAXFLOAT)options:NSStringDrawingTruncatesLastVisibleLine |NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeadingattributes:attributecontext:nil];height = rect.size.height;}return height;
}- (CGFloat)widthWithStringAttribute:(NSDictionary <NSString *, id> *)attribute {NSParameterAssert(attribute);CGFloat width = 0;if (self.length) {CGRect rect = [self boundingRectWithSize:CGSizeMake(MAXFLOAT, 0)options:NSStringDrawingTruncatesLastVisibleLine |NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeadingattributes:attributecontext:nil];width = rect.size.width;}return width;
}+ (CGFloat)aLineOfTextHeightWithStringAttribute:(NSDictionary <NSString *, id> *)attribute {CGFloat height = 0;CGRect rect    = [@"One" boundingRectWithSize:CGSizeMake(200, MAXFLOAT)options:NSStringDrawingTruncatesLastVisibleLine |NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeadingattributes:attributecontext:nil];height = rect.size.height;return height;
}@end

 

细节

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/news/458856.shtml
繁体地址,请注明出处:http://hk.pswp.cn/news/458856.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python笔记-python编程优化:常用原则和技术介绍

本人翻译自《Exper Python Programming》 Premature optimization is the root of all evil in programming -Donald Knuth 优化的三原则 让它跑起来先一个非常常见的错误就是在编写代码之初我们就开始对代码进行优化。让人伤心的是这通常做的都是无用功&#xff0c;很多软…

C++成员函数重载、覆盖和隐藏的区别

*************************************************** 更多精彩&#xff0c;欢迎进入&#xff1a;http://shop115376623.taobao.com *************************************************** C成员函数重载、覆盖和隐藏的区别class Base{public:void Walk(int x){ cout <<…

用fputc()函数以字符串形式写入字符到磁盘文件

2019独角兽企业重金招聘Python工程师标准>>> #include <stdio.h> #include <stdlib.h>int main(){FILE *fp;char ch;if((fpfopen("testfile", "a")) NULL){fprintf(stderr, "Error opening file.\n",fp);exit(1);}print…

android 浮动文字提示,Android实现自由拖动并显示文字的悬浮框

项目中需要实现一个状态显示的悬浮框&#xff0c;要求可以设置两种模式&#xff1a;拖动模式和不可拖动模式。实现效果图如下&#xff1a;实现步骤&#xff1a;1.首先要设置该悬浮框的基本属性&#xff1a;/*** 显示弹出框** param context*/SuppressWarnings("WrongConst…

Android4.2.2的Stagefright维护编解码器的数据流

这里是他们自己的源代码阅读点滴总结属性&#xff0c;转请注明出处&#xff0c;谢谢。欢迎和大家分享。qq:1037701636 email:gzzaigcn2012gmail.comAndroid源代码版本号Version&#xff1a;4.2.2; 硬件平台 全志A31前沿&#xff1a;在前面的博文中&#xff0c;基本提到的是stag…

PHP的安装

PHP的环境也是诸多服务器软件的必要因素之一&#xff0c;它是一个HTML内嵌式语言&#xff0c;在服务器端执行。由于PHP的开源高效化平台&#xff0c;所以搭建一个php环境是一个运维工程师必备的能力。现在lamp也有类似lnmp.org那种一键安装包&#xff0c;地址是http://yumlamp.…

android 横向铺满,Android开发全程记录(八)——设置ImageView显示的图片铺满全屏(适应魅族等不常见屏幕比例)...

为适应不同屏幕的手机&#xff0c;ImageView显示的图片可能不铺满屏幕&#xff0c;如果定高的话&#xff0c;两边可能会出现空白。魅族手机就会有这种情况&#xff0c;在其他手机里显示正常&#xff0c;在魅族手机里显示&#xff0c;图片左右两边会出现空白&#xff0c;为解决这…

tihs 关键字

//this关键词/*调用类中的属性 调用类中的方法或构造方法 调用当前对象&#xff0c;调用自己的方法&#xff0c;可以省略。 */ //http://blog.sina.com.cn/s/blog_71f6c1980100wtj4.html//this指当前对象自己public class Google{String s"hello";public Google(Stri…

良好的编程习惯

*************************************************** 更多精彩&#xff0c;欢迎进入&#xff1a;http://shop115376623.taobao.com *************************************************** 良好的编程习惯良好的习惯对于人的成长是非常重要的&#xff0c;良好的编程习惯对于我…

ntfs for mac使用注意事项有哪些?

2019独角兽企业重金招聘Python工程师标准>>> mac的用户有很多&#xff0c;一些用户朋友会发现自己的电脑是无法读写ntfs驱动器的。而ntfs驱动器又是一种常用的驱动器。面对这种情况我们可以选择用NTFS for Mac软件来帮助我们&#xff0c;它可以读写ntfs驱动器&#…

android音乐播放器文章,Android复习09【内容提供者、音乐播放器】

目 录PersonCpPersonCp.javainsert()ContentObserver音乐播放器1、添加读写权限1.1、动态权限授予(调用封装好的方法)2、获取音乐文件(MainActivity.java)2、Music.java(实体类)申请访问SD卡的权限设置适配器下拉刷新PersonCpPersonCp.javapackage cn.wangzg.personcp;import a…

程序员的业余项目

程序员的业余项目&#xff0c;我们也叫它 side project。 前几天&#xff0c;100offer 发起了一场活动叫 <寻找实干和坚持的技术力量>&#xff0c;他们是这么说的&#xff1a; 世界在被代码改变着&#xff0c;而我们在创造着代码。 仅仅是因为好玩&#xff0c;他开发了…

C语言的数组名和对数组名取地址

*************************************************** 更多精彩&#xff0c;欢迎进入&#xff1a;http://shop115376623.taobao.com *************************************************** 相信不少的C语言初学者都知道&#xff0c;数组名相当于指针&#xff0c;指向数组的首地…

小米 android 8,小米华为们谁最良心?10大手机厂商安卓8.0升级情况盘点

3月8日&#xff0c;谷歌放出了首个安卓9.0开发者预览版的固件包&#xff0c;不出意外的话&#xff0c;它的正式版会在今年正式亮相。但对广大安卓用户来说&#xff0c;想要立刻用上最新系统并非易事。目前来说&#xff0c;安卓碎片化问题依然严重&#xff0c;我们不妨现实点&am…

窥探Swift之数组安全索引与数组切片

在Swift中的数组和字典中下标是非常常见的&#xff0c;数组可以通过索引下标进行元素的查询&#xff0c;字典可以通过键下标来获取相应的值。在使用数组时&#xff0c;一个常见的致命错误就是数组越界。如果在你的应用程序中数组越界了&#xff0c;那么对不起&#xff0c;如果由…

大小端模式的快速判断方法

*************************************************** 更多精彩&#xff0c;欢迎进入&#xff1a;http://shop115376623.taobao.com *************************************************** 大小端的问题剖析&#xff1a; 嵌 入式系统开发者应该对Little-endian和Big-endian模…

【RAC】How to Proceed from Failed 11gR2 CRS Installation

Applies to: [ID 942166.1] Oracle Server – Enterprise Edition – Version: 11.2.0.1 to 11.2.0.2 – Release: 11.2 to 11.2 Generic UNIX Generic Linux Goal This goal of this note is to provide steps to proceed from failed 11gR2 Grid Infrastructure installat…

WinForm支持拖拽效果

有一个MSDN客户提问在WinForm中如何实现拖拽效果——比如在WinForm中有一个Button&#xff0c;我要实现的效果是拖拽这个Button到目标位置后生成一个该控件的副本。 其实这个操作主要分成三步走&#xff1a; 1&#xff09;确定被拖拽的对象&#xff1a; 这里是Button&#xff0…

win7 64位出现桌面右键鼠标显示忙碌

*************************************************** 更多精彩&#xff0c;欢迎进入&#xff1a;http://shop115376623.taobao.com *************************************************** 将下面绿色内容复制到txt文本中&#xff0c;然后另存为1.bat 双击运行即可 【针对64位…

android tee,Android 9.0的新增安全特性与TEE

Android P&#xff0c;预计将于 2018 年第三季度发布最终版本。特别是Android8.0以来&#xff0c;安全性是Android版本变更的一个重要因素。从安全性增强方面来看&#xff0c;本次Android9.0版本主要有以下几个方面&#xff1a;统一的指纹身份验证对话框Android P 中&#xff0…