简体   繁体   English

字体大小和类型不适用于CATextLayer

[英]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;
}

To change the font / font size of a CATextLayer, you have to assign values to the "font" and "fontSize" properties of the layer. 要更改CATextLayer的字体/字体大小,必须为图层的“font”和“fontSize”属性指定值。

Or you have to use an NSAttributedString in which case the values of that string object are used. 或者您必须使用NSAttributedString,在这种情况下使用该字符串对象的值。

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. 你使用的“sizeWithFont”调用是一个NSString添加,除了计算并返回一个CSSize,其中包含你给它的字体中文本的宽度和高度。 As you don't use the returned CGSize in your code, it does absolutely nothing. 由于您不在代码中使用返回的CGSize,因此它什么都不做。

Reference in the Apple docs . 参考Apple文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM