Я просто пытался добавить a 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;
}
Согласно документации, цвет текста по умолчанию для CATextLayer
белый. Белое на белом трудно увидеть.
попробуйте этот:
self.view = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];
[self.view setWantsLayer:YES];
self.view.backgroundColor = [UIColor blueColor];
Измените свой код на этот:
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 контроллера представления. Таким образом, вы знаете, что ваше представление загружено и действует, и теперь к нему могут быть добавлены слои.