Reputation: 343
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;
}
Upvotes: 12
Views: 13119
Reputation: 1759
You should (counter-intuitively) call textLayer.display()
or textLayer.displayIfNeeded()
after the initialization is complete or whenever you want it to draw text.
Upvotes: 5
Reputation: 511676
Here is an example that shows a view with a CATextLayer using a custom font with colored text.
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.
Upvotes: 4
Reputation: 13546
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.
Upvotes: 7
Reputation: 21
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];
Upvotes: 0
Reputation: 621
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...
Upvotes: 2
Reputation: 24466
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.
Upvotes: 5
Reputation: 135548
According to the docs, the default text color of a CATextLayer
is white. White on white is hard to see.
Upvotes: 0