关于ios:Core Text – 字形高度 | 珊瑚贝

Core Text – Height of glyph


我有 2 个属性字符串,比如 \\’A\\’ 和 \\’.\\’

我需要计算每个字符串的高度。目前返回的高度是相同的,它似乎返回给定字体中最高字符的最大可能高度(即使该字符不存在于字符串中)。

我想获得每个字符的确切像素高度,以便我可以调整它们周围的视图大小以贴合字符(字形)。我尝试过使用 CTFramesetterSuggestFrameSizeWithConstraints() 和 CTLineGetTypographicBounds() 但它返回的数字类似于属性字符串大小方法。

如果有任何关于如何进行此操作的提示,我们将不胜感激!


终于到了,你可以这样:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Create an attributed string
CTLineRef line = CTLineCreateWithAttributedString(_string);

// Get an array of glyph runs from the line
CFArrayRef runArray = CTLineGetGlyphRuns(line);

// loop through each run in the array      
CTRunRef run = ….

// Get the range of the run        
CFRange range = CFRangeMake…

// Use CTRunGetImageBounds                                  
CGRect glyphRect = CTRunGetImageBounds(run, context, range);

// glyphRect now contains the bounds of the glyph run, if the string is just 1 character you have the correct dimensions of that character.


来源:https://www.codenong.com/11739561/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9737.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?