With NSString, How to catelog a new interface to replace DrawInRect

63 views Asked by At

I wrote an interface "myDrawInRect" to replace the orginal "drawInRect" in NSString.

the code is like following:

.h file:

@interface NSString(TextDrawing)

-(CGSize) myDrawInRect:(CGRect)rect withFont:(UIFont *)font textColor:(UIColor*)textColor
         lineBreakMode:(NSLineBreakMode)lineBreakMode alignment:(NSTextAlignment)alignment;
@end

.m file:

@interface NSString(TextDrawing)

-(CGSize) myDrawInRect:(CGRect)rect withFont:(UIFont *)font textColor:(UIColor*)textColor
         lineBreakMode:(NSLineBreakMode)lineBreakMode alignment:(NSTextAlignment)alignment
{
    CGFloat screen_scale = [UIScreen mainScreen].scale;
    CGContextRef bitmapContext = CreateBitmapContext( rect.size.width * screen_scale, rect.size.height * screen_scale );

    /* ... some code to draw the bitmapContext; */

    CGImageRef image = CGBitmapContextCreateImage( bitmapContext );

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextScaleCTM( context, 1.0f, -1.0f );
    CGContextTranslateCTM( context, 0.0f, -CGRectGetHeight(realRect) );
    CGContextDrawImage( context, rect, image );

    CGContextRelease( bitmapContext );
    CGImageRelease( myImage );
}

@end

I invoked myDrawInRect in UILabel drawInRect, but nothing I can see on the label. Can anyone advise? thanks!

1

There are 1 answers

0
βhargavḯ On

Try this

CGImageRef image = CGBitmapContextCreateImage( bitmapContext );

CGContextRef context = UIGraphicsGetCurrentContext();

// MODIFY AND ADD FOLLOWING LINES
CGContextTranslateCTM( context, 0.0f, CGRectGetMaxY(realRect)*2 );
CGContextScaleCTM( context, 1.0f, -1.0f );

CGContextDrawImage( context, rect, image );

CGContextScaleCTM( context, 1.0f, -1.0f );
CGContextTranslateCTM( context, 0.0f, -CGRectGetMaxY(realRect)*2 );
// MODIFY UPTO THIS PONIT

CGContextRelease( bitmapContext );
CGImageRelease( myImage );