Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Font Size and Type not applying with CATextLayer

I am trying to modify the font properties of a text within a layer but it does not happen. Could anyone help out? Please find code below:

- (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;
}
like image 324
Armand Avatar asked Dec 19 '12 13:12

Armand


1 Answers

To change the font / font size of a CATextLayer, you have to assign values to the "font" and "fontSize" properties of the layer.

Or you have to use an NSAttributedString in which case the values of that string object are used.

The "sizeWithFont" call you use is an NSString addition that does nothing but calculate and return a CSSize with the width and height of the text you give it in the font you give it. As you don't use the returned CGSize in your code, it does absolutely nothing.

Reference in the Apple docs.

like image 105
David van Driessche Avatar answered Oct 30 '22 14:10

David van Driessche