2014-10-19 19 views
0

我正在嘗試更新優勝美地的應用程序,並且我得到的一個奇怪問題是自定義控件上的文本標籤正在更改字符 - 不是扭曲,而是從「打開」更改爲「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:文字]是:

- (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); 
} 
+0

你確定你已經安裝了你正在使用的字體? – 2014-10-19 02:34:45

+0

我應該 - 我正在使用boldSystemFontOfSize,其中Yosemite爲 - Helvetica Neue創建了一種新字體。我檢查了,並安裝了Helvetica Neue。 – 2014-10-19 04:55:49

+0

您需要顯示'+ [NSBezierPath bezierPathWithString:inFont:]'的代碼,這不是標準方法。它大概是由'NSBezierPath'上的某個類引入的,它幾乎肯定是問題的根源。 – 2014-10-19 07:20:35

回答

1

字形索引是特定於字體的。 appendBezierPathWithString:inFont:方法從核心文本(CTLineCTRun)獲取字形索引,但它不提供字體。據推測,Core Text正在使用默認字體。後來,它使用這些字形索引,但它傳遞了所需的字體,而不是Core Text使用的字體。所以,字形指數並不意味着同樣的事情。

我認爲解決的辦法是構建屬性串與字體的屬性,方法:

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

(通常情況下,你必須要小心使用屬性,這些屬性的核心文本就會明白,但我相信, NSFontAttributeName映射到kCTFontAttributeNameNSFont是免費橋接到CTFont。)

+0

謝謝!這是有道理的,你的答案完美無缺。我猜Core Text的默認字體不再是默認的系統字體。 – 2014-10-21 03:35:25

相關問題