Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you mask a react-native <View /> with any shape?

Tags:

react-native

It seems that all components in react-native are rectangles or rounded-rectangles (which can also represent a circle).

How do you mask a <View /> with an arbitrary shape like a hexagon?

like image 471
Gil Birman Avatar asked Sep 27 '22 18:09

Gil Birman


1 Answers

I came to the conclusion that this feature isn't available out of the box, so I implemented a native component in Objective-C called react-native-masked-view.

The basic idea is to use the mask property of the UIView class:

CALayer *mask = [CALayer layer];
mask.contents = (id)[_maskUIImage CGImage];
mask.frame = self.bounds; //TODO custom: CGRectMake(left, top, width, height);
self.layer.mask = mask;
self.layer.masksToBounds = YES;

and it works something like this in JavaScript:

<MaskedView maskImage="mask.png">
   ...
</MaskedView>
like image 197
Gil Birman Avatar answered Oct 06 '22 23:10

Gil Birman