8

レイヤー内のテキストのフォントプロパティを変更しようとしていますが、変更されません。誰か助けてもらえますか?以下のコードを見つけてください:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

if (self)
{

    // All HypnosisViews start with a clear background color

    [self setBackgroundColor:[UIColor clearColor]];
    [self setCircleColor:[UIColor lightGrayColor]];


    // Create the new layer object
    boxLayer = [[CATextLayer alloc] init];

    // Give it a size
    [boxLayer setBounds:CGRectMake(0.0, 0.0, 300.0, 85.0)];

    // Give it a location
    [boxLayer setPosition:CGPointMake(160.0, 350.0)];

    // Make half-transparent red the background color for the layer
    UIColor *reddish = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.75];

    // Get CGColor object with the same color values
    CGColorRef cgReddish = [reddish CGColor];
    [boxLayer setBackgroundColor:cgReddish];

    // Make it a sublayer on the view's layer
    [[self layer] addSublayer:boxLayer];

    NSString *text2 = @"You are me.";
    UIFont *font2 = [UIFont fontWithName:@"Times New Roman" size:10.0];
    [text2 sizeWithFont:font2];


    [boxLayer setString:text2];

}
return self;
}
4

1 に答える 1

11

CATextLayerのフォント/フォントサイズを変更するには、レイヤーの「font」プロパティと「fontSize」プロパティに値を割り当てる必要があります。

または、NSAttributedStringを使用する必要があります。この場合、その文字列オブジェクトの値が使用されます。

使用する「sizeWithFont」呼び出しはNSStringの追加であり、指定したフォントで指定したテキストの幅と高さを使用してCSSizeを計算して返します。返されたCGSizeをコードで使用しないため、まったく何もしません。

Appleドキュメントのリファレンス。

于 2012-12-19T21:46:19.693 に答える