use*_*743 12 iphone user-interface ios
我只是想CATextlayer在一个UIView图层中添加一个.但是,根据以下代码,我只得到CATextlayer要显示的背景颜色UIView,没有任何文字.只是想知道我错过了显示文字的内容.
任何人都可以提供提示/样品如何使用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;
}
Run Code Online (Sandbox Code Playgroud)
对于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];
Run Code Online (Sandbox Code Playgroud)
您可以在任何您喜欢的功能中添加此代码.特别是在这里正确分配字体是必要的,否则无论你设置什么textColor,你的CATextLayer都会呈现为黑色.
将您的代码更改为:
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];
Run Code Online (Sandbox Code Playgroud)
您还应该在视图控制器的-viewDidLoad中执行此操作.这样您就知道您的视图已加载且有效,现在可以添加图层.
| 归档时间: |
|
| 查看次数: |
11517 次 |
| 最近记录: |