0

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];
dev6546
  • 1,412
  • 7
  • 21
  • 40

2 Answers2

1

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

Michael Mangold
  • 1,741
  • 3
  • 18
  • 32
  • Any idea how to add them to a catextlayer? – dev6546 Sep 18 '12 at 18:02
  • It's really straight-forward - just create the `NSAttributedString` with all the styles you need and assign it to the `string` property of your `CATextLayer`! – Jay Nov 06 '12 at 17:15
0

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];
  • 1
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – J. Chomel Nov 03 '16 at 06:55