Xcode 使用CoreText的加扰输出

Xcode 使用CoreText的加扰输出,xcode,macos,core-graphics,core-text,Xcode,Macos,Core Graphics,Core Text,我正在尝试使用CoreText向子类NSView写入一些文本。这些字母相互重叠,字体太大了。下面是我的drawRect方法的代码。知道我做错了什么吗 编辑: 您需要将上下文的文本矩阵至少设置为CGAffineTransformity。酷!现在它起作用了。非常感谢你! - (void)drawRect:(NSRect)dirtyRect { NSGraphicsContext* contextRefNS = [NSGraphicsContext currentContext]; CGCon

我正在尝试使用CoreText向子类NSView写入一些文本。这些字母相互重叠,字体太大了。下面是我的drawRect方法的代码。知道我做错了什么吗

编辑:


您需要将上下文的文本矩阵至少设置为
CGAffineTransformity
。酷!现在它起作用了。非常感谢你!
- (void)drawRect:(NSRect)dirtyRect
{
  NSGraphicsContext* contextRefNS = [NSGraphicsContext currentContext];
  CGContextRef contextRef = (CGContextRef)[contextRefNS graphicsPort];

  [[NSColor whiteColor] set];
  NSRectFill([self bounds]);

  CTFontRef fontRef;
  CFStringRef fontNameRef;
  fontNameRef = CFStringCreateWithCString(0, "Verdana", kCFStringEncodingASCII);

  if (fontNameRef)
  {
    float fFontSize = 10.0f;
    fontRef = CTFontCreateWithName(fontNameRef, fFontSize, 0);
    CFRelease(fontNameRef);
  }

  CFStringRef stringRef = CFStringCreateWithCString(kCFAllocatorDefault, "TestText", kCFStringEncodingASCII);
  if (stringRef && fontRef)
  {
    CGColorRef cgColorRef = CGColorCreateGenericRGB(0,0,0,1);
    CFStringRef keys[] = { kCTFontAttributeName, kCTForegroundColorAttributeName };
    CFTypeRef values[] = { fontRef, cgColorRef };
    CFDictionaryRef attributes = CFDictionaryCreate(kCFAllocatorDefault, (const void**)&keys,(const void**)&values, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);

    unsigned char bAntiAlias = 1;
    CFAttributedStringRef attrStr = CFAttributedStringCreate(0, stringRef, attributes);
    CFRelease (attributes);
    if (attrStr)
    {
      CTLineRef line = CTLineCreateWithAttributedString(attrStr);
      if (line)
      {
        if (bAntiAlias)
          CGContextSetShouldAntialias(contextRef, true);
        else
          CGContextSetShouldAntialias(contextRef, false);
        CGContextSetShouldSmoothFonts(contextRef, true);
        CGContextSetTextPosition(contextRef, 100, 200);
        CTLineDraw(line, contextRef);
        CFRelease(line);
      }
      CFRelease(attrStr);
    }
    CFRelease(cgColorRef);
  }
  CFRelease(stringRef);
  CFRelease(fontRef);

}