views:

1469

answers:

5

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 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.

A: 

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.

drvdijk
A: 

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?

Nikolai Ruhe
I was just trying to get drawTextInRect to do *anything*. My real goal is to get the UILabel text to have a one pixel black outline border: http://stackoverflow.com/questions/1103148/how-do-i-make-uilabel-display-outlined-text
Monte
A: 

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. what you CAN do, is massage the parameter going INTO the draw function to alter the functionality... try something like this:

- (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!

kent
Yeah your suggestion yesterday seemed the best route to pursue - it's actually why I'm now asking about drawTextInRect. I only asked about the font size because I was trying to boil the example down - I figured if I could change the font's size I could also change it's fill and stroke and their colors for the outline text I need. While the snippet you posted above helps, I'm still near square one when comes to knowing how to make drawTextInRect add outlines to my UILabel subclass text. But I REALLY appreciate the help thus far!!!
Monte
A: 
/* 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);
}
kent
Just tried it and it drew an outline around the whole UILabel instead of around the text letters. Thanks though! Seriously, you guys are awesome.
Monte
A: 

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

Brock Woolf
I think the quote from Apple's documentation that I included in the question may have led me astray. The part that says "you can configure the current context further" made me think I could perhaps add my one pixel border to the individual letters displayed by the UILabel from within the drawTextInRect delegate. It's looking like that is not the case? I wish there were *setTextOutlineColor* and *setTextOutlineWidth* methods - THAT would be nice!!!
Monte
Again, I was just using CGContextSetFontSize as a simple example that I thought people must have used from within drawTextInRect. I figured if that could be done then I could also easily change the text outline stuff.
Monte