2010-06-18 14 views
10

Estaba tratando de agregar un CATextlayer en una capa UIView. Sin embargo, de acuerdo con el siguiente código, solo obtengo el color de fondo CATextlayer que se mostrará en el UIView, sin texto. Solo me pregunto qué me perdí para mostrar el texto.iPhone CATextLayer no muestra su texto

¿Alguien podría ofrecer una pista/muestra de cómo usar 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; 
    } 
+0

Sé que esta es una publicación anterior, pero el problema aquí es que agrega textlayer a la vista que creó manualmente, pero no agregó la vista a su vista actual. – GeneCode

Respuesta

0

De acuerdo con la documentación, el color del texto por defecto de un CATextLayer es blanco. Blanco sobre blanco es difícil de ver.

+0

¡Gracias por tu comentario! Después de cambiar el color a negro, aún no aparece el texto. ¿Alguna otra recomendación? – user268743

0

prueba este:

self.view = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)]; 
[self.view setWantsLayer:YES]; 
self.view.backgroundColor = [UIColor blueColor]; 
5

cambiar el código para esto:

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]; 

También debe hacer esto en -viewDidLoad del controlador de vista. De esa forma sabrá que su vista está cargada y es válida, y ahora puede tener capas agregadas.

+0

Parece que estaba intentando en una subclase UIView que no tiene un viewDidLoad. Probé tu código en el método 'layoutSubviews' y funcionó. No estoy seguro de cuál sería el mejor lugar para una UIView. – David

2

se puede personalizar el CLTextLayer Así,

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, no se olvidó de importar CoreText/CoreText.h en la vista de clase. Gracias ...

6

Para iOS 5 y más allá, uno puede usar CATextLayer de la siguiente manera:

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]; 

Usted puede agregar este código en cualquier función que desee. Especialmente aquí es necesaria la asignación correcta de la fuente , de lo contrario, su CATextLayer se representará como negro sin importar el texto que establezca.

0

Swift

Aquí es un ejemplo que muestra una vista con un CATextLayer usando una fuente personalizada con texto en color.

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) 
    } 
} 

Mi respuesta más completa es here.

0

Debería (de manera intuitiva) llamar a textLayer.display() o textLayer.displayIfNeeded() después de que se haya completado la inicialización o siempre que desee dibujar texto.

Cuestiones relacionadas