0

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

我需要像这样格式化一页文本:

标题(尺寸 20 HelveticaNeue-Light,黑色)

/n 换行符

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

2 回答 2

1

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

于 2012-09-18T17:52:21.077 回答
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];
于 2016-11-03T06:28:21.410 回答