我只是想在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;
}
答案 0 :(得分:7)
对于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];
您可以在任何您喜欢的功能中添加此代码。特别是在这里正确分配 font 是必要的,否则无论你设置什么textColor,你的CATextLayer都会呈现为黑色。
答案 1 :(得分: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中执行此操作。这样您就知道您的视图已加载且有效,现在可以添加图层。
答案 2 :(得分:4)
以下示例显示了使用带有彩色文本的自定义字体的CATextLayer视图。
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。
答案 3 :(得分: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_];
唐,忘了在视图类中导入CoreText / CoreText.h。感谢...
答案 4 :(得分:1)
您应该(反直觉地)在初始化完成后或在您希望它绘制文本时调用textLayer.display()
或textLayer.displayIfNeeded()
。
答案 5 :(得分:0)
根据文档,CATextLayer
的默认文本颜色为白色。白色的白色很难看到。
答案 6 :(得分:0)
试试这个:
self.view = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];
[self.view setWantsLayer:YES];
self.view.backgroundColor = [UIColor blueColor];