Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 为什么这篇文章会改变约塞米蒂的人物?_Objective C_Macos_Cocoa_Fonts - Fatal编程技术网

Objective c 为什么这篇文章会改变约塞米蒂的人物?

Objective c 为什么这篇文章会改变约塞米蒂的人物?,objective-c,macos,cocoa,fonts,Objective C,Macos,Cocoa,Fonts,我正在尝试更新优胜美地的一个应用程序,我遇到的一个奇怪的问题是,自定义控件上的文本标签正在更改字符-不是扭曲,而是从“开”更改为“KJ”,从“关”更改为“KBB”。这些文档都编码为UTF-8文件。如果有人有什么想法,我很乐意听听 有关守则: AKDrawStringAlignedInFrame(@"OFF", [NSFont boldSystemFontOfSize:0], NSCenterTextAlignment, NSIntegralRect(textRects[0])); 这要求: v

我正在尝试更新优胜美地的一个应用程序,我遇到的一个奇怪的问题是,自定义控件上的文本标签正在更改字符-不是扭曲,而是从“开”更改为“KJ”,从“关”更改为“KBB”。这些文档都编码为UTF-8文件。如果有人有什么想法,我很乐意听听

有关守则:

AKDrawStringAlignedInFrame(@"OFF", [NSFont boldSystemFontOfSize:0], NSCenterTextAlignment, NSIntegralRect(textRects[0]));
这要求:

void AKDrawStringAlignedInFrame(NSString *text, NSFont *font, NSTextAlignment alignment, NSRect frame) {
    NSCParameterAssert(font != nil);

    NSBezierPath *textPath = [NSBezierPath bezierPathWithString:text inFont:font];
    NSRect textPathBounds = NSMakeRect(NSMinX([textPath bounds]), [font descender], NSWidth([textPath bounds]), [font ascender] - [font descender]);

    NSAffineTransform *scale = [NSAffineTransform transform];
    CGFloat xScale = NSWidth(frame)/NSWidth(textPathBounds);
    CGFloat yScale = NSHeight(frame)/NSHeight(textPathBounds);
    [scale scaleBy:MIN(xScale, yScale)];
    [textPath transformUsingAffineTransform:scale];

    textPathBounds.origin = [scale transformPoint:textPathBounds.origin];
    textPathBounds.size = [scale transformSize:textPathBounds.size];

    NSAffineTransform *originCorrection = [NSAffineTransform transform];
    NSPoint centeredOrigin = NSRectFromCGRect(AFRectCenteredSize(NSRectToCGRect(frame), NSSizeToCGSize(textPathBounds.size))).origin;
    [originCorrection translateXBy:(centeredOrigin.x - NSMinX(textPathBounds)) yBy:(centeredOrigin.y - NSMinY(textPathBounds))];
    [textPath transformUsingAffineTransform:originCorrection];

    if (alignment != NSJustifiedTextAlignment && alignment != NSCenterTextAlignment) {
        NSAffineTransform *alignmentTransform = [NSAffineTransform transform];

        CGFloat deltaX = 0;
        if (alignment == NSLeftTextAlignment) deltaX = -(NSMinX([textPath bounds]) - NSMinX(frame));
        else if (alignment == NSRightTextAlignment) deltaX = (NSMaxX(frame) - NSMaxX([textPath bounds]));
        [alignmentTransform translateXBy:deltaX yBy:0];

        [textPath transformUsingAffineTransform:alignmentTransform];
    }

    [textPath fill];
}
而+[NSBezierPath bezierPathWithString:inFont:]只是

+ (NSBezierPath *)bezierPathWithString:(NSString *)text inFont:(NSFont *)font {
    NSBezierPath *textPath = [self bezierPath];
    [textPath appendBezierPathWithString:text inFont:font];
    return textPath;
}
最后,[appendBezierPathWithString:text]是:

- (void)appendBezierPathWithString:(NSString *)text inFont:(NSFont *)font {
  if ([self isEmpty]) [self moveToPoint:NSZeroPoint];

  NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:text];
  CTLineRef line = CTLineCreateWithAttributedString((CFAttributedStringRef)attributedString);

  CFArrayRef glyphRuns = CTLineGetGlyphRuns(line);
  CFIndex count = CFArrayGetCount(glyphRuns);

  for (CFIndex index = 0; index < count; index++) {
    CTRunRef currentRun = (CTRunRef)CFArrayGetValueAtIndex(glyphRuns, index);

    CFIndex glyphCount = CTRunGetGlyphCount(currentRun);

    CGGlyph glyphs[glyphCount];
    CTRunGetGlyphs(currentRun, CTRunGetStringRange(currentRun), glyphs);

    NSGlyph bezierPathGlyphs[glyphCount];
    for (CFIndex glyphIndex = 0; glyphIndex < glyphCount; glyphIndex++)
      bezierPathGlyphs[glyphIndex] = glyphs[glyphIndex];

    [self appendBezierPathWithGlyphs:bezierPathGlyphs count:glyphCount inFont:font];
  }

  CFRelease(line);
}
-(void)appendBezierPathWithString:(NSString*)文本信息:(NSFont*)字体{
if([self-isEmpty])[self-moveToPoint:NSZeroPoint];
NSAttributedString*attributedString=[[NSAttributedString alloc]initWithString:text];
CTLINEEF line=CTLineCreateWithAttributedString((CFAttributedStringRef)attributedString);
CFArrayRef glyphRuns=CTLineGetGlyphRuns(行);
CFIndex count=CFARRAYGETCUNT(运行次数);
对于(CFIndex index=0;index
字形索引特定于字体。
appendBezierPathWithString:inFont:
方法从核心文本(
CTLine
CTRun
)获取字形索引,但它不提供字体。据推测,核心文本使用的是默认字体。稍后,它将使用这些字形索引,但它将传递所需的字体,而不是核心文本使用的字体。所以,字形索引并不意味着相同的事情

我认为解决方案是在该方法中使用字体属性构造属性字符串:

NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:text attributes:@{ NSFontAttributeName: font }];

(通常,您必须小心使用核心文本可以理解的属性,但我相信
NSFontAttributeName
映射到
kctontattributename
,并且
NSFont
是免费桥接到
CTFont

你确定你已经安装了你正在使用的字体吗?我应该-我正在使用boldSystemFontOfSize,约塞米蒂有一种新的字体-Helvetica Neue。我检查了,安装了Helvetica Neue。您需要显示
+[NSBezierPath bezierPathWithString:inFont::
的代码,这不是标准方法。它大概是由
NSBezierPath
上的某个类别引入的,几乎可以肯定它是问题的根源。谢谢!这是有道理的,你的回答非常有效。我猜核心文本的默认字体不再是默认的系统字体了。