Question

I am trying to add my CATextLayer frame to a UIScrollView in order to get some scrolling. I have been trying to use the technique mentioned here (How can i make my CATextLayer object Scrollable like UITextView?) with no success.

Actually due to the (suggested) 3 lines I've added to my code my CATextLayer does not display anymore.

I have attached my code below and noted these 3 lines. Perhaps someone can help me troubleshoot this or even propose a better way to approach this :-)

// Create the scrool view (FIRST LINE ADDED)
UIScrollView* scrollLayer = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, 500.0, 500.0)];

// Create the new layer object
boxLayer = [[CATextLayer alloc] init];

// Give it a size
[boxLayer setBounds:CGRectMake(0.0, 0.0, 500.0, 500.0)];

// Give it a location
[boxLayer setPosition:CGPointMake(300.0, 350.0)];

// Make half-transparent red the background color for the layer
UIColor *reddish = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.1];

// Get CGColor object with the same color values
CGColorRef cgReddish = [reddish CGColor];
[boxLayer setBackgroundColor:cgReddish];

// Make it a sublayer on the view's layer
[self.view.layer addSublayer:boxLayer];

// Create string
NSString *text2 = @"The article was about employment.\nHe leafed through it in an instant.\nHis feeling of anxiety resurfaced and he closed the magazine.\n\n-Hm…, he breathed.\n\n-Have you been looking for work long?, asked the stranger at his side.\nThe article was about employment.\nHe leafed through it in an instant.\nHis feeling of anxiety resurfaced and he closed the magazine.\n\n-Hm…, he breathed.\n\n-Have you been looking for work long?, asked the stranger at his side.\nThe article was about employment.\nHe leafed through it in an instant.\nHis feeling of anxiety resurfaced and he closed the magazine.\n\n-Hm…, he breathed.\n\n-Have you been looking for work long?, asked the stranger at his side.";


// Set font type
[boxLayer setFont:@"MarkerFelt-Thin"];

// Set font size
[boxLayer setFontSize:20.0];

// Text is left justified
[boxLayer setAlignmentMode:kCAAlignmentLeft];

// Text is wrapped
boxLayer.wrapped = YES;

// Assign string to layer
[boxLayer setString:text2];

// Define the text size (SECOND LINE ADDED)
scrollLayer.contentSize = CGSizeMake(500, 1000);

// Set the text layer as a sub layer of the scroll view (THIRD LINE ADDED)
[scrollLayer.layer addSublayer:boxLayer];
Was it helpful?

Solution

Pursuant to our discussion on another discussion, I wonder if you should even be adding CATextLayer objects at all. You can add UILabel objects, for example. For example, let's add a series of UILabel objects to our scroll view:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    if (!self.addedLabels)
    {
        [self addLabelsToScrollView:self.scrollView];
        self.addedLabels = YES;
    }
}

- (void)addLabelsToScrollView:(UIScrollView *)scrollView
{
    NSArray *gettysburgAddress = @[
                                   @"Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal.",
                                   @"Now we are engaged in a great civil war, testing whether that nation, or any nation, so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this.",
                                   @"But, in a larger sense, we can not dedicate, we can not consecrate, we can not hallow this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us—that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion—that we here highly resolve that these dead shall not have died in vain—that this nation, under God, shall have a new birth of freedom—and that government of the people, by the people, for the people, shall not perish from the earth."
                                  ];

    CGFloat y = 0.0;
    UIFont *font = [UIFont fontWithName:@"MarkerFelt-Thin" size:20.0];
    CGSize maxSize = CGSizeMake(scrollView.frame.size.width, 10000.0);

    for (NSString *line in gettysburgAddress)
    {
        CGSize labelSize = [line sizeWithFont:font constrainedToSize:maxSize lineBreakMode:NSLineBreakByWordWrapping];
        CGRect labelFrame = CGRectMake(0.0, y, labelSize.width, labelSize.height);
        UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];
        label.font = font;
        label.text = line;
        label.numberOfLines = 0;
        [scrollView addSubview:label];

        y += labelSize.height + 16.0;
    }

    scrollView.contentSize = CGSizeMake(scrollView.contentSize.width, y);
}

Or even easier, rather than adding UILabel objects to a UIScrollView, you could just use a UITextView:

- (void)updateTextView
{
    NSArray *gettysburgAddress = @[
                                   @"Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal.",
                                   @"Now we are engaged in a great civil war, testing whether that nation, or any nation, so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this.",
                                   @"But, in a larger sense, we can not dedicate, we can not consecrate, we can not hallow this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us—that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion—that we here highly resolve that these dead shall not have died in vain—that this nation, under God, shall have a new birth of freedom—and that government of the people, by the people, for the people, shall not perish from the earth."
                                  ];

    NSString *text = [gettysburgAddress componentsJoinedByString:@"\n\n"];

    self.textView.text = text;
    self.textView.font = [UIFont fontWithName:@"MarkerFelt-Thin" size:20.0];
    self.textView.editable = NO;
}

It all comes down to whether you really need to use a CATextLayer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top