4

この質問に続いて、1行のテキストを垂直方向に揃える方法を理解しました。

CGRect boundingBox = CTFontGetBoundingBox(font);

//Get the position on the y axis
float midHeight = self.frame.size.height / 2;
midHeight -= boundingBox.size.height / 2;

CGPathAddRect(path, NULL, CGRectMake(0, midHeight, self.frame.size.width, boundingBox.size.height));

ただし、このコードは1行のコンテキストでのみ機能します。フレームが複数行の文字列(ソフトまたはハードの改行を含む)を生成する場合、そのコードは機能しません。

4

0 に答える 0