【发布时间】:2013-05-23 21:43:50
【问题描述】:
我正在创建一个 pdf 报告,该报告基于一系列 UITextFields 中的文本呈现帧。目前我正在使用以下一系列方法。这些变量用于跟踪下一帧的位置,并基于正在考虑的当前UITextField 中的文本长度以及渲染时每行的长度约为 pdf doc 上的 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' 的行上可能会有更多字符)。
我想知道是否有任何方法可以准确计算渲染文本实际使用的行数,从而使我能够准确计算下一帧的确切位置。或感激地收到任何其他建议。
【问题讨论】:
标签: objective-c ipad pdf ios6