Catextlayer字符串大小和颜色

时间:2012-09-18 15:28:27

标签: objective-c ios fonts catextlayer

我无法理解如何定义不同大小,颜色以及如何添加换行符的catextlayer的某些部分?

我需要格式化一页文字:

标题(大小20 HelveticaNeue-Light,黑色)

/ n换行

Para 1(尺寸15 HelveticaNeue-Light,黑色)

/ n换行

第2段(尺寸15 HelveticaNeue-Light,自定义颜色)

目前我所拥有的是标题文字,有人可以帮助我吗?

    CATextLayer *TextLayer = [CATextLayer layer];
    TextLayer.bounds = CGRectMake(0.0f, 0.0f, 245.0f, 290.0f);
    TextLayer.string = @"Title";
    CTFontCreateWithName((CFStringRef)@"HelveticaNeue-Light", 0.0, NULL);
    TextLayer.backgroundColor = [UIColor blackColor].CGColor;
    TextLayer.position = CGPointMake(162.0, 250.0f);
    TextLayer.wrapped = YES;
    TextLayer.fontSize = 20;
    [self.view.layer addSublayer:TextLayer];

2 个答案:

答案 0 :(得分:1)

听起来你想使用属性字符串。如果您有Apple Developer帐户,可以在文档中了解更多信息。

答案 1 :(得分:0)

以下是应该有效的代码:

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle.alignment = NSTextAlignmentLeft;
NSAttributedString *string= [[NSAttributedString alloc] initWithString:@"#We enjoy rftef gdfg dfgdfg dfgdfg dfgdfg dgdfg dgdfg gdfg dfgdfg dgdfg"
                                  attributes:[NSDictionary
                                              dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:11],
                                              NSFontAttributeName,
                                              paragraphStyle, NSParagraphStyleAttributeName,nil]];



CATextLayer *subtitle1Text = [[CATextLayer alloc] init];
[subtitle1Text setFont:@"Helvetica-Bold"];
[subtitle1Text setFontSize:18];
[subtitle1Text setFrame:overlayLayer1.bounds];
[subtitle1Text setString:string];