日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

计算一行文本的高度

發(fā)布時(shí)間:2023/12/19 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 计算一行文本的高度 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

計(jì)算一行文本的高度

?

說(shuō)明

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

?

源碼

// // 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

?

細(xì)節(jié)

?

總結(jié)

以上是生活随笔為你收集整理的计算一行文本的高度的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。