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

10,293

Solution 1

/* 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);
}

Solution 2

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!

Solution 3

You can try setting the font of the UILabel:

UIFont *font = [UIFont fontWithName:@"Courier" size:32];
[self setFont: font]; 
[super drawTextInRect:rect];

Solution 4

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.

Solution 5

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?

Share:
10,293
Monte Hurd
Author by

Monte Hurd

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." - Brian Kernighan

Updated on June 04, 2022

Comments

  • Monte Hurd
    Monte Hurd almost 2 years

    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.

  • Monte Hurd
    Monte Hurd almost 15 years
    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: stackoverflow.com/questions/1103148/…
  • Monte Hurd
    Monte Hurd almost 15 years
    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 Hurd
    Monte Hurd almost 15 years
    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 Hurd
    Monte Hurd almost 15 years
    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 Hurd
    Monte Hurd almost 15 years
    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.