0

애니메이션 텍스트 레이블을 만들려고했는데 갑자기이 이상한 문제가 발생했습니다. 보기에 CATextLayer를 사용하여 간단한 레이블을 쓰려고합니다. 보시다시피, NSAttributedString 클래스의 sizeWithAttributes :를 사용하여 텍스트 프레임을 계산하려고했습니다. 이것은 레이어를 사용하여 동일한 속성의 문자열을 표시하더라도 CATextLayer에 완벽하게 맞지 않는 프레임을 제공합니다.CATextLayer 문자 모양이 잘 리거나 찢어졌습니다.

이 홀수 글꼴 렌더링 문제가 발생하는 이유에 대한 통찰력은 인정 될 것입니다.

#import "AppDelegate.h" 

@implementation AppDelegate 

@synthesize view; 

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
{ 
view.layer = [CALayer layer]; 
[view setWantsLayer:YES]; 

CATextLayer *textLayer = [CATextLayer layer]; 

textLayer.fontSize = 12; 
textLayer.position = CGPointMake(100, 50); 
[view.layer addSublayer:textLayer]; 

NSFont *font = [NSFont fontWithName:@"Helvetica-Bold" size:12]; 
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil]; 
NSString *helloString = @"This is a long string"; 
NSAttributedString *hello = [[NSAttributedString alloc] initWithString:helloString attributes:attrs]; 
NSSize stringBounds = [helloString sizeWithAttributes:attrs]; 
stringBounds.width = ceilf(stringBounds.width); 
stringBounds.height = ceilf(stringBounds.height); 

textLayer.bounds = CGRectMake(0, 0, stringBounds.width, stringBounds.height); 
textLayer.string = hello; 

} 

@end 

답변

0

CATextLayer는 Cocoa (실험을 바탕으로 한 예상 추측)가 아닌 CoreText를 사용하여 글리프를 렌더링합니다.

CoreText 및 CTTypesetter를 사용하여 크기를 계산하는 방법을 보려면이 샘플 코드를 확인하십시오. 이로 인해 훨씬 ​​정확한 결과를 얻을 수있었습니다 : https://github.com/rhult/height-for-width

+0

작동 했습니까? 그런 다음 질문을 대답으로 표시 할 수 있습니다. –