Question

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];
Was it helpful?

Solution 2

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];
}

OTHER TIPS

Here's how to do it in iOS 8 (or 7+) based on @Merlevede's answer with a correction and using new API:

    NSString *string = ....
    NSDictionary *attributes = ....
    CGSize size = [string sizeWithAttributes:attributes];

    CGRect r = CGRectMake(rect.origin.x,
                          rect.origin.y + (rect.size.height - size.height)/2.0,
                          rect.size.width,
                          size.height);


    [string drawInRect:r withAttributes:attributes];

Swift 3 solution:

extension NSString {
    func drawVerticallyCentered(in rect: CGRect, withAttributes attributes: [String : Any]? = nil) {
        let size = self.size(attributes: attributes)
        let centeredRect = CGRect(x: rect.origin.x, y: rect.origin.y + (rect.size.height-size.height)/2.0, width: rect.size.width, height: size.height)
        self.draw(in: centeredRect, withAttributes: attributes)
    }
}

Swift version in form of the extension, based on @WillLarche and @Merlevede answers.

extension CGRect {
    func verticalCenteredRectForString(string:NSString,withAttributes attributes : [String:AnyObject])->CGRect {
        let size = string.sizeWithAttributes(attributes)
        return CGRectMake(self.origin.x, self.origin.y + (self.size.height-size.height)/2.0 , self.size.width, size.height)
    }
}

Usage

let myString = NSString(string:"Some text")
let centeredRect = customRect.verticalCenteredRectForString(myString,attrParams)
myString.drawInRect(centereRect, withAttributes : attrParams)

P.S. I know that name of method could be better ;)

Swift 4 Solution:

extension NSString {

    func drawVerticallyCentered(in rect: CGRect, withAttributes attributes: [NSAttributedStringKey : Any]? = nil) {
        let size = self.size(withAttributes: attributes)
        let centeredRect = CGRect(x: rect.origin.x, y: rect.origin.y + (rect.size.height-size.height)/2.0, width: rect.size.width, height: size.height)
        self.draw(in: centeredRect, withAttributes: attributes)
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top