One Quickie


Drawing a string centered in a rectangle (Graphics->General)
- (void) drawText: (NSString *) text
   centeredInRect: (CGRect) rect
             font: (UIFont *) font {
    CGSize textSize = [text sizeWithFont: font
                            constrainedToSize: rect.size
                            lineBreakMode: NSLineBreakByWordWrapping];

    // Center text rect inside of |rect|.
    CGRect textRect = CGRectMake (CGRectGetMidX(rect) - textSize.width / 2.0,
                                  CGRectGetMidY(rect) - textSize.height / 2.0,
                                  textSize.width, textSize.height);

    [text drawInRect: textRect
          withFont: font
          lineBreakMode: NSLineBreakByWordWrapping
          alignment: NSTextAlignmentCenter];
} // drawText



borkware home | products | miniblog | rants | quickies | cocoaheads
Advanced Mac OS X Programming book

webmonster@borkware.com