2010-06-18 59 views
10

我只是试图在UIView图层中添加CATextlayer。但是,根据以下代码,我只能得到CATextlayer的背景色,并且不显示任何文字,而是显示在UIView中。只是想知道我错过了什么来显示文本。iPhone CATextLayer不显示其文本

任何人都可以提供提示/示例如何使用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

我知道这是旧帖子,但这里的问题是您将textlayer添加到您手动创建的视图中,但未将视图添加到当前视图。 – GeneCode 2017-01-05 05:27:52

回答

0

根据文档,CATextLayer的默认文本颜色是白色。白色的白色很难看清楚。

+0

感谢您的发布! 将颜色更改为黑色后,仍然没有显示文字。还有什么建议? – user268743 2010-06-18 09:13:57

0

试试这个:

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

更改您的代码如下:

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

你也应该在视图控制器的-viewDidLoad可以这样做。这样你就知道你的视图已经加载并且有效,现在可以添加图层了。

+0

看起来他正在尝试在没有viewDidLoad的UIView子类中。我在'layoutSubviews'方法中试过了你的代码,它工作。不确定UIView的最佳位置是什么。 – David 2011-05-23 02:29:54

2

您可以自定义CLTextLayer这样,

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

唐,T忘记导入CoreText/CoreText.h在您的视图类。谢谢...

6

对于iOS 5及以后,可以使用CATextLayer如下:

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

你可以在你喜欢的任何函数中添加此代码。特别是在这里,字体的正确赋值是必要的,否则无论您设置了什么textColor,您的CATextLayer都将呈现为黑色。

0

夫特

这里是表示使用与彩色文本自定义字体一个CATextLayer的视图的例子。

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

我更全面的回答是here

0

在初始化完成或您希望绘制文本时,您应该(反直觉地)调用textLayer.display()textLayer.displayIfNeeded()