8

「A」と「.」という 2 つの属性文字列があります。

これらの文字列のそれぞれの高さを計算する必要があります。現在、返される高さは両方で同じです。特定のフォントで最も高い文字の最大可能高さを返すようです (その文字が文字列に存在しない場合でも)。

これらの各文字の正確なピクセルの高さを取得して、文字 (グリフ) にぴったり合うようにビューのサイズを変更できるようにしたいと考えています。CTFramesetterSuggestFrameSizeWithConstraints() と CTLineGetTypographicBounds() を使用してみましたが、属性付き文字列のサイズ メソッドと同様の数値が返されます。

これを行う方法についてのヒントをいただければ幸いです。

4

1 に答える 1

12

最終的にそこにたどり着き、次のようにすることができます:

// 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.
于 2012-08-07T12:31:32.713 に答える