0

I have a UITextView in a collapsable TableViewCell. In the UITextView I want a CAGradientLayer to fade out the above en bottom text.

I made a UIView with the UITextView in it. This is all in the cell

UIView in Cell

I don't know how to set the code, in the cell or in the view controller.

My test code is from Github: Fun with Mask by Ewan Davids.

[self createGradientMask];

Where should I put the (void) method? In the TableViewCell or ViewController?

    - (void)createGradientMask
        {
            //creating our gradient mask
        CAGradientLayer *maskLayer = [CAGradientLayer layer];

        //this is the anchor point for our gradient, in our case top left. setting it in the middle (.5, .5) will produce a radial gradient. our startPoint and endPoints are based off the anchorPoint
        maskLayer.anchorPo = CGPointZero;

        //The line between these two points is the line our gradient uses as a guide
        //starts in bottom left
        maskLayer.startPoint = CGPointMake(0.0f, 1.0f);

        //ends in top right
        maskLayer.endPoint = CGPointMake(1.f, 0.0f);

        //setting our colors - since this is a mask the color itself is irrelevant - all that matters is the alpha. A clear color will completely hide the layer we're masking, an alpha of 1.0 will completely show the masked view.
        UIColor *outerColor = [UIColor colorWithWhite:1.0 alpha:0.0];
        UIColor *innerColor = [UIColor colorWithWhite:1.0 alpha:1.0];

        //an array of colors that dictatates the gradient(s)
        maskLayer.colors = @[(id)outerColor.CGColor, (id)outerColor.CGColor, (id)innerColor.CGColor, (id)innerColor.CGColor];

        //these are percentage points along the line defined by our startPoint and endPoint and correspond to our colors array. The gradient will shift between the colors between these percentage points.
        maskLayer.locations = @[@0.0, @0.15, @0.5, @1.0f];

        maskLayer.bounds = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 

    CGRectGetHeight(self.view.bounds));

            self.underTextView.layer.mask = maskLayer;
        }
  • In the TableViewCell I doesn't recognise the view property.
  • In the ViewController I doesn't recognise the UITextView property,

The QuartzCore.framework has been added.

Robert Karl
  • 7,598
  • 6
  • 38
  • 61
SwingerDinger
  • 276
  • 1
  • 7
  • 21

1 Answers1

1

Put the gradient code inside the tableview data source method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // code to customize cell here
}

At this point have have a chance to customize the cell view, such as color, text, layers and subviews and so on...

Shams Ahmed
  • 4,498
  • 4
  • 21
  • 27