2012-12-19 26 views
6

我试图修改图层内文本的字体属性,但它不会发生。任何人都可以帮忙吗?请看以下代码:字体大小和类型不适用于CATextLayer

- (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; 
} 

回答

7

要更改CATextLayer的字体/字体大小,你必须分配值“字体”与层“fontSize的”属性。

或者您必须使用NSAttributedString,在这种情况下使用该字符串对象的值。

您使用的“sizeWithFont”调用是一个NSString添加,它除了计算并返回一个CSSize以及您赋予其字体的文本的宽度和高度。由于您在代码中不使用返回的CGSize,它绝对没有任何用处。

参考Apple docs

+0

这太棒了。谢谢。有效。 – Armand

+0

完美 - 如果它是你正在寻找的,请将此标记为正确的答案,以便其他人可以看到这已被解决! –

+0

当然,我会但如何去做呢?我是这个网站的新手。 关于CATextLayer的问题:有没有什么办法可以让图层的角落变圆呢?你将如何为图层内的文本划线? – Armand

相关问题