2010-06-18 37 views
10

Tôi chỉ đơn giản là cố gắng thêm một CATextlayer trong một lớp UIView. Tuy nhiên, theo đoạn mã sau, tôi chỉ nhận được màu nền của CATextlayer được hiển thị trong UIView, mà không có bất kỳ văn bản nào. Chỉ cần tự hỏi những gì tôi bị mất để hiển thị văn bản.iPhone CATextLayer không hiển thị văn bản của nó

Có ai có thể đưa ra gợi ý/mẫu cách sử dụng CATextlayer không?

- (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

Tôi biết đây là bài cũ, nhưng vấn đề ở đây là bạn thêm textlayer vào chế độ xem mà bạn đã tạo theo cách thủ công, nhưng bạn không thêm chế độ xem vào chế độ xem hiện tại của mình. – GeneCode

Trả lời

0

Theo tài liệu, màu văn bản mặc định của CATextLayer là màu trắng. Trắng trên trắng là khó nhìn thấy.

+0

Cảm ơn bài đăng của bạn! Sau khi tôi đổi màu thành đen, vẫn không hiển thị văn bản. Bất kỳ đề xuất nào khác? – user268743

0

thử này một:

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

Thay đổi mã của bạn như thế này:

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

Bạn cũng nên làm điều này trong -viewDidLoad bộ điều khiển quan điểm của. Bằng cách đó, bạn biết chế độ xem của mình đã được tải và hợp lệ và giờ đây có thể thêm các lớp vào đó.

+0

Có vẻ như anh ta đang cố gắng trong một lớp con UIView không có viewDidLoad. Tôi đã thử mã của bạn trong phương thức 'layoutSubviews' và nó đã hoạt động. Bạn không chắc chắn nơi tốt nhất cho một UIView là gì. – David

2

Bạn có thể tùy chỉnh CLTextLayer Như thế này,

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 quên để nhập khẩu CoreText/CoreText.h trong xem lớp học của bạn. Cảm ơn ...

6

Đối với iOS 5 và xa hơn nữa, người ta có thể sử dụng CATextLayer như sau:

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

Bạn có thể thêm mã này trong bất kỳ chức năng bạn muốn. Đặc biệt ở đây, việc gán chính xác phông chữ là cần thiết, nếu không CATextLayer của bạn sẽ được hiển thị dưới dạng màu đen bất kể bạn đã đặt textColor nào.

0

Swift

Dưới đây là ví dụ hiển thị chế độ xem với CATextLayer sử dụng phông chữ tùy chỉnh có văn bản màu.

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

câu trả lời đầy đủ hơn của tôi là here.

0

Bạn nên (phản trực giác) gọi textLayer.display() hoặc textLayer.displayIfNeeded() sau khi khởi tạo xong hoặc bất cứ khi nào bạn muốn vẽ văn bản.

Các vấn đề liên quan