一連のテキストに基づいてフレームをレンダリングする PDF レポートを作成していますUITextField
。現在、私は以下の一連の方法を使用しています。変数は、次のフレームの位置を追跡するために使用され、現在UITextField
考慮されているテキストの長さと、レンダリング時の各行の長さが PDF ドキュメントで約 97 文字であるという仮定に基づいています。
-(void) drawTextObservationComment;
{
//pdfLineHeight is the height of size 12 rendered text
pdfLineHeight = 15;
CGContextRef summaryContext = UIGraphicsGetCurrentContext();
UIFont *font = [UIFont fontWithName:@"arial" size:12];
CGContextSetFillColorWithColor (summaryContext, [UIColor blackColor].CGColor);
//pdfCurrentLine is the y-axis coordinate from which to begin the new frame
CGRect textRect = CGRectMake(60, pdfCurrentLine, 650, 300);
NSString *myString = self.observationComment.text;
[myString drawInRect:textRect withFont:font lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentLeft];
//checks for the no of carriage returns within the text field
NSInteger numberOfLines = [[myString componentsSeparatedByString:@"\n"] count];
//the new pdfCurrentLine value is the previous value
// + the no of new lines based on text length
// + the no lines based on the no of carriage returns within text field
// + 40 as standard gap
pdfCurrentLine = pdfCurrentLine + (([observationComment.text length]/97)*pdfLineHeight) + ((numberOfLines - 1) * pdfLineWidth) + 40;
}
これはある程度機能しますが、完全に正確ではないことを認識しています。フレーム内のレンダリングされたテキストは、CGRect
多くの場合 97 文字ではありません (ただし、通常はこの数字前後で 10 文字前後です)。これは、入力されたテキストによって異なります (たとえば、文字「i」は細くなるため、「i」が多い行にはより多くの文字が表示される場合があります)。
レンダリングされたテキストが実際に使用する行数を正確に計算する方法があるかどうかを知りたいので、次のフレームの正確な位置を正確に計算できます。または、感謝して受け取ったその他のアドバイス。