【问题标题】:how to show words in the layer如何在图层中显示单词
【发布时间】:2011-06-28 09:21:04
【问题描述】:

我想在一层上写一些单词。但是当我创建图层时,我找不到相应的API来将文字附加到图层。

layer2 = [CALayer layer]; 
[layer2 setBackgroundColor:[[UIColor grayColor] CGColor]];
layer2.bounds = CGRectMake(0, 0, 60,40);
layer2.position = CGPointMake(25,25);//     
layer2.contentsRect = layer2.bounds;
layer2.contents=@"Hello World~~"; //It's nothing in the showing layer .
[self.layer addSublayer:layer2];

【问题讨论】:

    标签: iphone objective-c cocoa-touch calayer


    【解决方案1】:

    contents 属性仅适用于 CGImageRef。如果你想在上面使用文字,请使用CATextLayer

    示例用法

    CATextLayer * textLayer = [CATextLayer layer];
    textLayer.backgroundColor = [[UIColor darkGrayColor] CGColor];
    textLayer.foregroundColor = [[UIColor whiteColor] CGColor];
    textLayer.bounds = CGRectMake(0, 0, 60, 40);
    textLayer.position = CGPointMake(25, 25);
    textLayer.string = @"Hello World";
    textLayer.font = CGFontCreateWithFontName(CFSTR("Helvetica"));
    textLayer.fontSize = 15.0;
    
    [self.window.layer addSublayer:textLayer];
    

    当然,我正在使用CGFontCreateWithFontName 泄漏内存。您可以通过将其分配给变量并稍后释放它来解决此问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-18
      • 1970-01-01
      • 2012-05-09
      相关资源
      最近更新 更多