私はこれに答えてスタック全体を検索してきましたが、トピックにはたくさんの質問があります。コア・テキストに合わせてUIViewとCGPathを自動拡張する
私はストーリーボード(プロトタイプセル内)で固定サイズのカスタムUIViewを持っています。 UIViewをサブクラス化し、drawRectメソッドを上書きしました。それは基本的にフォーマットされた文字列を一緒に置き、その後、私はそうのようにそれを描く:
罰金だと動作し、私は十分に大きいデフォルトサイズを作れば、それは複数行のテキストを処理します// now for the actual drawing
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetShadowWithColor(context,
CGSizeMake(0, 1),
0,
[UIColor whiteColor].CGColor);
CGMutablePathRef path = CGPathCreateMutable(); //1
CGPathAddRect(path, NULL, self.bounds);
// flip the coordinate system
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CTFramesetterRef framesetter =
CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)stringToDraw); //3
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
CTFrameDraw(frame, context); //4
。
CGPathはすべてのUIViewを使用します。
私はCGPathの幅を固定したままにしたいが、本質的に無制限のテキストに対応するために高さを拡大したいが、今はちょうどカットオフになる(パス/ビューが十分に大きくないため)
CTFramesetterSuggestFrameSizeWithConstraintsを使用して試してみました。誰かが私が何をする必要があるかを達成するためのコードを開発するのを手伝ってもらえますか?描画する文字列と使用するフォントを知る
ありがとう@nicolasthenoz私がフィットするよう拡張する必要がある他の理由はありましたが、テキストの直後/下に他の要素を配置する予定だったからです。また、私のテーブルのセルも、テキストのために必要以上に大きく拡大します。 –
'sizeWithFont'はCoreTextでは正確に動作しません。 –