0

CGRect下面的代码是我在制作 pdf 文档时如何绘制一些文本的示例。我需要计算使用comment1CGRect绘制的确切大小,以便comment2comment3等都从页面上的正确位置开始。

变量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...... ....在计算contentSize.heightsizeWithAttributes时假设的宽度是多少?CGSizesizeWithAttributes

或者有没有更好的方法来做到这一点?

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 回答