Can I override the UILabel drawTextInRect method to resize UILabel text?

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

“By the time this method is called, the current graphics context has already been set using the default environment and the color of the text to draw. In your overridden method, you can additionally configure the current context and then call super for the actual drawing, or you can do the drawing yourself. If you yourself render the text, you should not refer to super. "

But the example below from my subclass of UILabel (which, as I confirmed, is called) does not change the size of the text, no matter what text size I specify. Am I grabbing the right context or perhaps missing something more?

- (void)drawTextInRect:(CGRect)rect{

    CGContextRef theContext = UIGraphicsGetCurrentContext();
    CGContextSetFontSize(theContext, 32.0); // <-- doesn't change text size

    [super drawTextInRect:rect];
}

. - , , , , , , .

+3
6
/* 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);
}
+4

, ... , appKit , InterfaceBuilder, . , , - , , ... - :

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

, nikolai : , setFont!

+2

UILabel:

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

, [super drawTextInRect: rect] , CGContextSetFontSize. , , , , -.

+1

drawTextInRect UILabel ( ), . , setFont: method minimumFontSize?

+1

. (IBOutlet) UILabel.

Then you can set the font UILabel, courage, etc., using the UILabel setText method :

+1
source

Source: https://habr.com/ru/post/1712281/


All Articles