0

I would like to use a CATextLayer to display some partially bolded text in the form of a NSAttributedString - but would still like to use the convenience of IB for positioning.

Is there a way to drop in a CATextLayer with interface builder? Or what is the next best solution?

trapper
  • 11,716
  • 7
  • 38
  • 82

1 Answers1

3

You could configure a UIView subclass in IB then set its layerClass to CATextLayer in code:

+ (Class)layerClass;
{
    return [CATextLayer class];
}

In the view's init method(s) configure your CATextLayer properties.

To access the layer's properties:

CATextLayer *textLayer = (CATextLayer *)self.layer;
textLayer.string = @"Foo";
// etc...
FluffulousChimp
  • 9,157
  • 3
  • 35
  • 42
  • So I have attempted this method but as my new subclass inherits from UIView it doesn't know about the CATextLayer methods. `self.myView.string = @"test";` gives `Property 'string' not found on object of type 'MyView *'` – trapper Mar 13 '12 at 20:03
  • 1
    The property is on the layer, not the view. See additional code sample in answer edit. – FluffulousChimp Mar 13 '12 at 20:07
  • Ahh yes, ok that is working now. Although thinking about it I could just create my CATextLayer in code then add it as a sublayer to my positioning UIView. Save the trouble of subclassing UIView – trapper Mar 13 '12 at 20:56