0

When I tried to add image as CATextLayer content, text is not shown. Only image is shown. This is my code which I have placed in viewDidLoad() method,

    UIImage *uiimage = [UIImage imageNamed:@"flying.png"];
    CATextLayer *textLayer = [CATextLayer layer];
    textLayer.frame = CGRectMake(144, 42, 76, 21);
    textLayer.font = CFBridgingRetain([UIFont boldSystemFontOfSize:18].fontName);
    textLayer.fontSize = 18;
    textLayer.foregroundColor = [UIColor redColor].CGColor;
    textLayer.backgroundColor = [UIColor yellowColor].CGColor;
    textLayer.alignmentMode = kCAAlignmentCenter;
    textLayer.string = @"Hello world";
    textLayer.contents = (id)uiimage.CGImage;
    [self.view.layer addSublayer:textLayer];

Please help to solve the issue.

Rashwan L
  • 38,237
  • 7
  • 103
  • 107
rishu1992
  • 1,414
  • 3
  • 14
  • 33
  • Why not just creat two layers, one for the image and one for the text? – Eric Qian Sep 26 '16 at 04:43
  • That works. But wanted to know if setting the content of the text layer works. – rishu1992 Sep 26 '16 at 04:46
  • 2
    Hardly think so, I think setting the string is a convinience way of setting the contents of CATextLayer, so if you set the contents again, the text will be overridden. However it is just my guess, havn't verified. – Eric Qian Sep 26 '16 at 04:56
  • I am agree with Eric Qian's answer, and if you real want to show the `image` and the `text` I suggest you add the text to the `img` by `photoshop` – aircraft Sep 26 '16 at 05:19

0 Answers0