Need To Trap No CGSize on Occasional Nil values in IOS Building a PDF

544 views Asked by At

I am creating 2 columns down a page. The left column is the "Label", the Right column is the "Data". I'm building and getting the CGSize on the "Data" side because it may wrap down considerably. The "Label" side is pre-configure text and is written after the data.

But the problem comes in if there is no data in an object (nil?). Then the next data write does not get moved down. And the next "Label" get's over written. Here is my code. It repeats for about 20 different attributes.

Thanks in advance. I'm just looking for the cleanest way to #1 check for no "size" (no data was entered by the user), and/or #2 assign a minimum 'size'. here is the code that essentially is going on ..

size = [[self.currentLoad otherTrailers] sizeWithFont:textFont constrainedToSize:CGSizeMake(maxDataWidth, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
[[self.currentLoad otherTrailers]drawInRect:CGRectMake(kMargin+maxLabelWidth+kColumnMargin, currentPageY, maxDataWidth, maxHeight) withFont:textFont lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentLeft];
[@"Other Trailers" drawInRect:CGRectMake(kMargin, currentPageY, maxLabelWidth, maxHeight) withFont:textFont lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentRight];
currentPageY += size.height;
1

There are 1 answers

1
rmaddy On

Get and check the value:

NSString *value = [self.currentLoad otherTrailers];
CGFloat height = 15; // or some appropriate minimum value
if (value.length) {
    size = [value sizeWithFont:textFont constrainedToSize:CGSizeMake(maxDataWidth, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
    [value drawInRect:CGRectMake(kMargin+maxLabelWidth+kColumnMargin, currentPageY, maxDataWidth, maxHeight) withFont:textFont lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentLeft];
    height = size.height;
}

[@"Other Trailers" drawInRect:CGRectMake(kMargin, currentPageY, maxLabelWidth, maxHeight) withFont:textFont lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentRight];
currentPageY += height;