12

I was simply trying to add a CATextlayer in an UIView layer. However, according to the following code, I only get the CATextlayer's background color to be displayed in the UIView, without any text. Just wonder what I missed to display the text.

Could anyone offer a hint/sample how to use CATextlayer?

  - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
        if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
            // Custom initialization

            CATextLayer *TextLayer = [CATextLayer layer];
            TextLayer.bounds = CGRectMake(0.0f, 0.0f, 100.0f, 100.0f);
            TextLayer.string = @"Test";
            TextLayer.font = [UIFont boldSystemFontOfSize:18].fontName;
            TextLayer.backgroundColor = [UIColor blackColor].CGColor;
            TextLayer.wrapped = NO;

            //TextLayer.backgroundColor = [UIColor blueColor];
            self.view = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];
            self.view.backgroundColor = [UIColor blueColor];
            [self.view.layer addSublayer:TextLayer];
            [self.view.layer layoutSublayers];

        }
        return self;
    }
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
user268743
  • 343
  • 1
  • 4
  • 14
  • I know this is old post, but the problem here is that you add textlayer to the view that you manually created, but you didn't add the view to your current view. – GeneCode Jan 05 '17 at 05:27

7 Answers7

7

For iOS 5 and beyond, one can use CATextLayer as follows:

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 = @"BAC";
[self.view.layer addSublayer:textLayer];

You can add this code in any function you like. Specially here correct assignment of font is necessary, otherwise your CATextLayer will be rendered as black no matter what textColor you set.

NightFury
  • 13,436
  • 6
  • 71
  • 120
5

Change your code to this:

CATextLayer *TextLayer = [CATextLayer layer];
TextLayer.bounds = CGRectMake(0.0f, 0.0f, 100.0f, 100.0f);
TextLayer.string = @"Test";
TextLayer.font = [UIFont boldSystemFontOfSize:18].fontName;
TextLayer.backgroundColor = [UIColor blackColor].CGColor;
TextLayer.position = CGPointMake(80.0, 80.0f);
TextLayer.wrapped = NO;
[self.view.layer addSublayer:TextLayer];

You should also be doing this in the view controller's -viewDidLoad. That way you know your view is loaded and valid and can now have layers added to it.

Matt Long
  • 24,438
  • 4
  • 73
  • 99
  • Looks like he was trying in a UIView subclass which doesn't have a viewDidLoad. I tried your code in the 'layoutSubviews' method and it worked. Not sure what the best place for a UIView would be. – David May 23 '11 at 02:29
5

You should (counter-intuitively) call textLayer.display() or textLayer.displayIfNeeded() after the initialization is complete or whenever you want it to draw text.

Aleksey Gureiev
  • 1,729
  • 15
  • 17
4

Swift

Here is an example that shows a view with a CATextLayer using a custom font with colored text.

enter image description here

import UIKit
class ViewController: UIViewController {

    @IBOutlet weak var myView: UIView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Attributed string
        let myAttributes = [
            NSFontAttributeName: UIFont(name: "Chalkduster", size: 30.0)! , // font
            NSForegroundColorAttributeName: UIColor.cyanColor()             // text color
        ]
        let myAttributedString = NSAttributedString(string: "My text", attributes: myAttributes )
        
        // Text layer
        let myTextLayer = CATextLayer()
        myTextLayer.string = myAttributedString
        myTextLayer.backgroundColor = UIColor.blueColor().CGColor
        myTextLayer.frame = myView.bounds
        myView.layer.addSublayer(myTextLayer)
    }
} 

My fuller answer is here.

Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
2

You can customize the CLTextLayer Like this,

    CATextLayer *aTextLayer_= [[CATextLayer alloc] init];

aTextLayer_.frame =CGRectMake(23.0, 160.0, 243.0, 99.0);

aTextLayer_.font=CTFontCreateWithName( (CFStringRef)@"Courier", 0.0, NULL);

    aTextLayer_.string = @"You string put here";

aTextLayer_.wrapped = YES;

aTextLayer_.foregroundColor = [[UIColor greenColor] CGColor];

aTextLayer_.fontSize = 15.f;

   aTextLayer_.backgroundColor = [UIColor blackColor].CGColor;

aTextLayer_.alignmentMode = kCAAlignmentCenter;

[self.view.layer addSublayer:aTextLayer_];

Don,t forgot to import CoreText/CoreText.h in your view-class. Thanks...

Arunjack
  • 621
  • 1
  • 6
  • 4
0

According to the docs, the default text color of a CATextLayer is white. White on white is hard to see.

Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
0

try this one:

self.view = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];
[self.view setWantsLayer:YES];
self.view.backgroundColor = [UIColor blueColor];
iDev
  • 23,310
  • 7
  • 60
  • 85