Can UILabel's drawTextInRect method be overridden to change the size of UILabel's text?

Can UILabel's drawTextInRect method be overridden to change the size of UILabel's text?

Apple's documentation for drawTextInRect seems to indicate this is possible:

"By the time this method is called, the current graphics context is already configured with the default environment and text color for drawing. In your overridden method, you can configure the current context further and then invoke super to do the actual drawing or you can do the drawing yourself. If you do render the text yourself, you should not invoke super."

But the example below from my UILabel subclass (which I've confirmed is getting called) doesn't cause the text size to change no matter what text size I specify. Am I grabbing the right context or perhaps missing something bigger?

- (void)drawTextInRect:(CGRect)rect{      CGContextRef theContext = UIGraphicsGetCurrentContext();     CGContextSetFontSize(theContext, 32.0); // <-- doesn't change text size      [super drawTextInRect:rect]; } 

Note - the text size isn't the only thing I need to change about the text, but if I could get the text size to change I'm pretty sure the rest of the changes I need to make would be easy.

Slideup\Animate next page with jquery


Scrolling a webpage inside UIWebView
/* use CGContextSetLineWidth() and CGContextSetStrokeColorWithColor()   to adjust your outline settings: directly preceding StrokeRect() */   - (void)drawTextInRect:(CGRect)rect{      [super drawTextInRect:rect]; // let super do the work      CGContextStrokeRect(UIGraphicsGetCurrentContext(),rect); } 

TabBarController NAvigationController and UIViewController

Having trouble decrypting in C# something encrypted on iPhone using RSA


Is there a nine-patch loader for iPhone?
i thought we had this one yesterday....
you can trust that the appKit superclass is going to set the fonts and colors and stuff to the settings you set up in InterfaceBuilder, so trying to override this behaviour is totally futile.

How do I detect, via javascript in Safari Mobile, if my iPhone application is installed?
what you CAN do, is massage the parameter going INTO the draw function to alter the functionality...

Sending iphone videos to a server
try something like this:.
Can give different color in CGContextSetRGBFillColor?
- (void)drawTextInRect:(CGRect)rect{      CGFloat newWidth = rect.size.width * 0.75;    // 3/4 the original width     CGFloat newHeight = rect.size.height * 0.812; // and a little less tall. 

CGRect newRect = CGRectMake(rect.origin.x,rect.origin.y,newWidth,newHeight); [super drawTextInRect:newRect]; // draw text into the NEW rect! }
additionally, nikolai is correct: if you want to change the font, then call setFont!.


You can try setting the font of the UILabel:.
UIFont *font = [UIFont fontWithName:@"Courier" size:32]; [self setFont: font];  [super drawTextInRect:rect]; 


I think the call to [super drawTextInRect:rect] also sets the font size, undoing your call to CGContextSetFontSize.

If you want this, you'll probably have to do all the drawing yourself instead of calling super's implementation..


The drawTextInRect method of UILabel sets the text size (along with the font and other settings) itself, overriding your size.

If you want to change the font size, why don't you just use the setFont: method or minimumFontSize property?.


You don't do this in delegate methods.

You need to get a pointer (IBOutlet) to the UILabel.. You can then set the UILabel's font, boldness, etc using the UILabel's setText: method.

70 out of 100 based on 70 user ratings 670 reviews