cocoa绘画中文

来源:互联网 发布:mac虚拟机win10镜像 编辑:程序博客网 时间:2024/05/01 01:57

绘画中文:

Quartz中CGContextShowTextAtPoint不支持中文,用以下方式绘画

    NSGraphicsContext *gc = [NSGraphicsContextgraphicsContextWithGraphicsPort:context flipped:NO];

        [NSGraphicsContextsetCurrentContext:gc];

        [gcflushGraphics];

        [strtextdrawAtPoint:origin withAttributes:nil];

        [NSGraphicsContextrestoreGraphicsState];



以下是将字符串内容转换为矢量路径图形

- (NSBezierPath *) makePathFromString: (NSString *) string
                              forFont: (NSFont *) font
{
    NSTextView *textview;
    textview = [[NSTextView allocinit];
        
    [textview setString: string];
    [textview setFont: font];
        
    NSLayoutManager *layoutManager;
    layoutManager = [textview layoutManager];
        
    NSRange range;
    range = [layoutManager glyphRangeForCharacterRange:
                      NSMakeRange (0, [string length])
                                                         actualCharacterRangeNULL];
    NSGlyph *glyphs;
    glyphs = (NSGlyph *) malloc (sizeof(NSGlyph)
                                 * (range.length * 2));
    [layoutManager getGlyphs: glyphs range: range];
        
    NSBezierPath *path;
    path = [NSBezierPath bezierPath];
        
    [path moveToPointNSMakePoint (20.020.0)];
    [path appendBezierPathWithGlyphs: glyphs
                                                    count: range.length inFont: font];
        
    free (glyphs);
    [textview release];
        
    return (path);