核心文本 - 字形的高度 [英] Core Text - Height of glyph

查看:221
本文介绍了核心文本 - 字形的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个属性字符串说'A'和'。'。

I have 2 attributed strings say 'A' and '.'

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

I need to calculate the height of each of these strings. Currently the height returned is the same for both, it seems to return the maximum possible height for the tallest character in a given font (even if that character isn't present in the string).

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

I would like to get the exact pixel height for each of these characters, so that I can resize a view around them that fits the character (glyph) snugly. I've tried using CTFramesetterSuggestFrameSizeWithConstraints() and CTLineGetTypographicBounds() but it returns a number similar to the attributed strings size method.

感谢任何提示如何去做这个!

Would appreciate any tips on how to go about doing this!

推荐答案

到那里结束,你可以这样做:

Got there in the end, you can do it like this:

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

这篇关于核心文本 - 字形的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆