为什么我的文字是从下往上绘制的?

发布于 2024-10-27 00:52:53 字数 1723 浏览 1 评论 0原文

在我的自定义视图中,我有以下代码:

-(void) drawRect:(CGRect) rect
{
    // Drawing code   
    CGContextRef context = UIGraphicsGetCurrentContext();

    ![NSString *myString = @"Hello World, This is for\nhttp://www.stackoverflow.com";
    // Prepare font
    CTFontRef font = CTFontCreateWithName(CFSTR("Times"), 12, NULL);

    // Create an attributed string
    CFStringRef keys[] = { kCTFontAttributeName };
    CFTypeRef values[] = { font };
    CFDictionaryRef attr = CFDictionaryCreate(NULL, (const void **)&keys, (const void **)&values,
                                              sizeof(keys) / sizeof(keys[0]), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
    CFAttributedStringRef attrString = CFAttributedStringCreate(NULL, (CFStringRef)myString, attr);
    CFRelease(attr);

    // Draw the string
    //CTLineRef line = CTLineCreateWithAttributedString(attrString);
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString);
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, self.frame);
    CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
    //CGContextSetTextMatrix(context, CGAffineTransformIdentity); // this line is wrong,  use the below line
    CGContextSetTextMatrix(context, CGAffineTransformMakeScale(1.0, -1.0)); 
    CGContextSetTextPosition(context, 20, 12);
    CTFrameDraw(frame, context);

    // Clean up
    CFRelease(path);
    CFRelease(frame);
    CFRelease(attrString);
    CFRelease(font);
}

但是,看起来像这样:

https://i.sstatic.net/ iGonN.png

如何使文本向下,并且从左上角开始?

In my custom view, I have the following code:

-(void) drawRect:(CGRect) rect
{
    // Drawing code   
    CGContextRef context = UIGraphicsGetCurrentContext();

    ![NSString *myString = @"Hello World, This is for\nhttp://www.stackoverflow.com";
    // Prepare font
    CTFontRef font = CTFontCreateWithName(CFSTR("Times"), 12, NULL);

    // Create an attributed string
    CFStringRef keys[] = { kCTFontAttributeName };
    CFTypeRef values[] = { font };
    CFDictionaryRef attr = CFDictionaryCreate(NULL, (const void **)&keys, (const void **)&values,
                                              sizeof(keys) / sizeof(keys[0]), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
    CFAttributedStringRef attrString = CFAttributedStringCreate(NULL, (CFStringRef)myString, attr);
    CFRelease(attr);

    // Draw the string
    //CTLineRef line = CTLineCreateWithAttributedString(attrString);
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString);
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, self.frame);
    CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
    //CGContextSetTextMatrix(context, CGAffineTransformIdentity); // this line is wrong,  use the below line
    CGContextSetTextMatrix(context, CGAffineTransformMakeScale(1.0, -1.0)); 
    CGContextSetTextPosition(context, 20, 12);
    CTFrameDraw(frame, context);

    // Clean up
    CFRelease(path);
    CFRelease(frame);
    CFRelease(attrString);
    CFRelease(font);
}

However, that looks like this:

https://i.sstatic.net/iGonN.png

How can I make the text go down, and also start from the top left?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零度° 2024-11-03 00:52:53

这是副本&粘贴我的drawRect:方法之一,它工作得很好。

CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState( ctx );

CGContextSetTextMatrix( ctx, CGAffineTransformIdentity );

CGContextTranslateCTM( ctx, rect.origin.x, rect.origin.y );
CGContextScaleCTM( ctx, 1.0f, -1.0f );
CGContextTranslateCTM( ctx, rect.origin.x, - ( rect.origin.y + rect.size.height ) );

CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect( path, NULL, rect );

CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString( __labelData.attributedString );  
CTFrameRef frame = CTFramesetterCreateFrame( framesetter, CFRangeMake( 0, 0 ), path, NULL );    

CTFrameDraw( frame, ctx );

CGPathRelease( path );  
CFRelease( frame );
CFRelease( framesetter );

CGContextRestoreGState( ctx );  

只需将 __labelData.attributedString 替换为您的属性字符串即可。

Here's the copy & paste of one of my drawRect: methods, which does work perfectly.

CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState( ctx );

CGContextSetTextMatrix( ctx, CGAffineTransformIdentity );

CGContextTranslateCTM( ctx, rect.origin.x, rect.origin.y );
CGContextScaleCTM( ctx, 1.0f, -1.0f );
CGContextTranslateCTM( ctx, rect.origin.x, - ( rect.origin.y + rect.size.height ) );

CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect( path, NULL, rect );

CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString( __labelData.attributedString );  
CTFrameRef frame = CTFramesetterCreateFrame( framesetter, CFRangeMake( 0, 0 ), path, NULL );    

CTFrameDraw( frame, ctx );

CGPathRelease( path );  
CFRelease( frame );
CFRelease( framesetter );

CGContextRestoreGState( ctx );  

Just replace __labelData.attributedString with your attributed string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文