博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IOS core text计算文本高度及最大宽度
阅读量:7165 次
发布时间:2019-06-29

本文共 1628 字,大约阅读时间需要 5 分钟。

转自:
 
- ( CGSize ) measureFrame: ( CTFrameRef ) frame forContext: (CGContext *) cgContext
{//frame为排版后的文本
CGPathRef framePath = CTFrameGetPath (frame);
CGRect frameRect = CGPathGetBoundingBox (framePath);
 
CFArrayRef lines = CTFrameGetLines (frame);
CFIndex numLines = CFArrayGetCount (lines);
 
CGFloat maxWidth = 0 ;
CGFloat textHeight = 0 ;
 
// Now run through each line determining the maximum width of all the lines.
// We special case the last line of text. While we've got it's descent handy,
// we'll use it to calculate the typographic height of the text as well.
CFIndex lastLineIndex = numLines - 1 ;
for ( CFIndex index = 0 ; index < numLines; index++)
{
CGFloat ascent, descent, leading, width;
CTLineRef line = ( CTLineRef ) CFArrayGetValueAtIndex (lines, index);
width = CTLineGetTypographicBounds (line, &ascent,  &descent, &leading);
 
if (width > maxWidth)
{
maxWidth = width;
}
 
if (index == lastLineIndex)
{
// Get the origin of the last line. We add the descent to this
// (below) to get the bottom edge of the last line of text.
CGPoint lastLineOrigin;
CTFrameGetLineOrigins (frame, CFRangeMake (lastLineIndex, 1 ), &lastLineOrigin);
 
// The height needed to draw the text is from the bottom of the last line
// to the top of the frame.
textHeight =  CGRectGetMaxY (frameRect) - lastLineOrigin. y + descent;
}
}
 
// For some text the exact typographic bounds is a fraction of a point too
// small to fit the text when it is put into a context. We go ahead and round
// the returned drawing area up to the nearest point.  This takes care of the
// discrepencies.
return CGSizeMake ( ceil (maxWidth), ceil (textHeight));
}

转载地址:http://wymwm.baihongyu.com/

你可能感兴趣的文章
设置Linux内核参数 /etc/sysctl.conf
查看>>
超简单jquery tab切换
查看>>
Nagios(五)——配置PNP图表分析
查看>>
系统集成项目管理工程师真题模拟上午题(3套)
查看>>
pnp4nagios的安装配置
查看>>
OrbitDB-Peer 2 Peer Database using CRDTs
查看>>
全屏状态下层级问题(z-index)
查看>>
Using a SecureCRT 链接远程主机
查看>>
python写的好的博客链接
查看>>
linux shell wait process finished,等待线程执行结束
查看>>
MySQL内存表的特性与使用介绍
查看>>
男人帮,帮男人找爱情
查看>>
linux添加HIVE环境变量
查看>>
Secure Delivery Center快速入门指南(二):交付软件包
查看>>
MyEclipse Spring开发教程:使用基本的Spring功能(三)
查看>>
算法训练 未名湖边的烦恼
查看>>
数据库的备份和恢复
查看>>
2019假期作业(一)
查看>>
1.登陆一下好吗??
查看>>
java集合
查看>>