2010-06-18 14 views
10

Po prostu próbowałem dodać CATextlayer w warstwie UIView. Jednak zgodnie z poniższym kodem, dostaję tylko kolor tła CATextlayer, który ma być wyświetlany w UIView, bez żadnego tekstu. Zastanawiam się, co przegapiłem, aby wyświetlić tekst.iPhone CATextLayer nie wyświetla swojego tekstu

Czy ktokolwiek może podać podpowiedź/próbkę, jak korzystać z 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

Wiem, że to stary post, ale problem polega na tym, że dodajesz tekst do utworzonego ręcznie widoku, ale nie dodałeś go do bieżącego widoku. – GeneCode

Odpowiedz

0

Zgodnie z docs, kolor domyślny tekst z CATextLayer jest biały. Białe na białym są słabo widoczne.

+0

Dzięki za twój post! Po zmianie koloru na czarny nadal nie widać tekstu. Jakieś dalsze zalecenia? – user268743

0

próbować ten jeden:

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

zmienić swój kod do tego:

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

Należy również robić to w -viewDidLoad kontrolera widoku za. W ten sposób wiesz, że Twój widok jest załadowany i prawidłowy i może teraz mieć dodane do niego warstwy.

+0

Wygląda na to, że próbował w podklasie UIView, która nie ma viewDidLoad. Próbowałem Twojego kodu w metodzie "layoutSubviews" i działało. Nie wiem, jakie byłoby najlepsze miejsce dla UIView. – David

2

Można dostosować CLTextLayer Jak to,

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 zapomniał importować CoreText/CoreText.h w widoku klasy. Dzięki ...

6

Dla iOS 5 i poza nią, można użyć CATextLayer następująco:

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

Możesz dodać ten kod w dowolnej funkcji chcesz. Zwłaszcza tutaj wymagane jest poprawne przypisanie czcionki , w przeciwnym razie twoja warstwa CATextLayer będzie renderowana jako czarna, niezależnie od ustawionego koloru tekstu.

0

Swift

Oto przykład, który pokazuje widok z CATextLayer stosując niestandardową czcionkę z kolorowym tekstem.

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

Moja odpowiedź brzmi here pełniejsze.

0

Powinieneś (wbrew intuicji) nazywamy textLayer.display() lub textLayer.displayIfNeeded() po zakończeniu inicjowania lub gdy chcesz go wyciągnąć tekst.

Powiązane problemy