我正在嘗試更新優勝美地的應用程序,並且我得到的一個奇怪問題是自定義控件上的文本標籤正在更改字符 - 不是扭曲,而是從「打開」更改爲「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);
}
你確定你已經安裝了你正在使用的字體? – 2014-10-19 02:34:45
我應該 - 我正在使用boldSystemFontOfSize,其中Yosemite爲 - Helvetica Neue創建了一種新字體。我檢查了,並安裝了Helvetica Neue。 – 2014-10-19 04:55:49
您需要顯示'+ [NSBezierPath bezierPathWithString:inFont:]'的代碼,這不是標準方法。它大概是由'NSBezierPath'上的某個類引入的,它幾乎肯定是問題的根源。 – 2014-10-19 07:20:35