計算一行文本的高度

計算一行文本的高度

?

說明

有時候我們需要知道指定的幾行文本的高度,此工具用于解決此種問題。

?

源碼

//
//  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
英文地址,請注明出處:http://en.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…