Madhu Madhu - 4 months ago 128
iOS Question

drawInRect: withAttributes text center vertically or put some padding

I'm using drawInRect : withAttributes to add text to a pdf in iOS 7. I need to vertically center the text inside the CGRect or at least I need to keep some gap/padding between the CGRect border and text. Otherwise text look too closer to the box. Is there any attribute to do that? If not what is the best way to do that? Below is my code.

I tried NSBaselineOffsetAttributeName , but it only increases the gap between each line, but not the gap to the border of the rect.


Thanks

CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(currentContext, bgColor.CGColor);
CGRect renderingRect = CGRectMake(startPoint.x, startPoint.y, width, height);
CGContextFillRect(currentContext, renderingRect);

NSDictionary *attributes = @{ NSFontAttributeName: font,
NSForegroundColorAttributeName: fgColor,
};

[textToDraw drawInRect:renderingRect withAttributes:attributes];

Answer

First you need to calculate the height of the text, with this information and the height of your bounding rectangle, you can easily compute the new rectangle to center the text.

I will share a piece of code I use to center vertically. In my case I use a different drawInRect function (drawInRect:withFont...) and I use sizeWithFont to calculate the size of the text. YOu would either adapt this code to use the functions you're already using (with attributes), or either use the functions I'm posting here.

UIFont *font = [UIFont systemFontOfSize:14];
CGSize size = [text sizeWithFont:font];
if (size.width < rect.size.width)
{
    CGRect r = CGRectMake(rect.origin.x, 
                          rect.origin.y + (rect.size.height - size.height)/2, 
                          rect.size.width, 
                          (rect.size.height - size.height)/2);
    [text drawInRect:r withFont:font lineBreakMode:UILineBreakModeClip alignment:UITextAlignmentLeft];
}
Comments