0

アニメーション テキスト ラベルを作成しようとしていたところ、突然、この奇妙な問題が発生しました。ビューで CATextLayer を使用して単純なラベルを書き出そうとしています。ご覧のとおり、NSAttributedString クラスの sizeWithAttributes: を使用して、テキストの枠を計算しようとしました。これにより、レイヤーを使用して同じ属性文字列を表示しているにもかかわらず、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
4

1 に答える 1

0

CATextLayer は、Cocoa ではなく CoreText を使用してグリフをレンダリングします (実験に基づく推測)。

このサンプル コードをチェックして、CoreText と CTTypesetter を使用してサイズを計算する方法を確認してください。これにより、より正確な結果が得られました: https://github.com/rhult/height-for-width

于 2013-05-27T09:34:10.097 に答える