简体   繁体   English

使用NSString,如何对新接口进行类别记录以替换DrawInRect

[英]With NSString, How to catelog a new interface to replace DrawInRect

I wrote an interface "myDrawInRect" to replace the orginal "drawInRect" in NSString. 我编写了一个接口“ myDrawInRect”来替换NSString中原始的“ drawInRect”。

the code is like following: 代码如下:

.h file: .h文件:

@interface NSString(TextDrawing)

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

.m file: .m文件:

@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. 我在UILabel drawInRect中调用了myDrawInRect,但是在标签上看不到任何内容。 Can anyone advise? 有人可以建议吗? thanks! 谢谢!

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 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM