애니메이션 텍스트 레이블을 만들려고했는데 갑자기이 이상한 문제가 발생했습니다. 보기에 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
작동 했습니까? 그런 다음 질문을 대답으로 표시 할 수 있습니다. –