Question

I'm having trouble getting my head around how to define certain sections of a catextlayer in different sizes, colours and how to add line breaks?

I need to format a page of text like so:

Title (in size 20 HelveticaNeue-Light, black)

/n line break

Para 1 (in size 15 HelveticaNeue-Light, black)

/n line break

para 2 (in size 15 HelveticaNeue-Light, custom colour)

At the moment all I have is the title text, can anyone help me out?

    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];
Was it helpful?

Solution

Sounds like you want to use attributed strings. If you have an Apple Developer account you can learn more in the docs.

OTHER TIPS

Here is the code that should work:

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];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top