0

以下のコードはCGRect、pdf ドキュメントを作成するときにテキストを描画するために使用する方法の例です。comment2comment3などのものがすべてページの正しい場所から始まるように、 comment1CGRectで描画されているものの正確なサイズを計算する必要があります。

変数currentLineは、ページの最後がどこにあるかを追跡CGRectします。短いコメントでは問題なく機能しますが、後続のコメントが重なる長いコメントでは機能しません。フォントとフォント サイズは適切です。

textRect = CGRectMake(60, currentLine, 650, 300);
myString =  self.comments1.text;
[myString drawInRect:textRect withAttributes:_textAttributesNotBold ];

CGSize contentSize = [myString sizeWithAttributes: @{NSFontAttributeName: [UIFont fontWithName:@"Arial" size:12]}];
currentLine =  currentLine +  contentSize.height;

CGRectMake問題は650 幅の使用にあるのだろうか。CGSizeこれは....では考慮されていないようですsizeWithAttributescontentSize.heightを計算するときにCGSize....はどのような幅を想定していますか?sizeWithAttributes

または、これを行うより良い方法はありますか?

4

1 に答える 1

1

これが良い方法かどうかはわかりませんが、うまくいくようです:

NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@"Arial" size:12]};
CGRect rect = [comment1.text boundingRectWithSize:CGSizeMake(650, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];

CGRect textRect = CGRectMake(60, currentLine, 650, rect.size.height);
[comment1.text drawInRect:textRect withAttributes:attributes ];
currentLine =  currentLine +  rect.size.height;
于 2015-03-01T19:35:42.903 に答える