Skip to content

Commit

Permalink
feat(ios): update Ti.UI.View.borderRadius to accept number, array and…
Browse files Browse the repository at this point in the history
… string
  • Loading branch information
vijaysingh-axway authored and sgtcoolguy committed Jul 7, 2020
1 parent 0c1de27 commit 34b3a93
Showing 1 changed file with 73 additions and 7 deletions.
80 changes: 73 additions & 7 deletions iphone/TitaniumKit/TitaniumKit/Sources/API/TiUIView.m
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,9 @@ - (void)frameSizeChanged:(CGRect)frame bounds:(CGRect)bounds
if (backgroundRepeat) {
[self renderRepeatedBackground:backgroundImage];
}
if ([proxy valueForUndefinedKey:@"borderRadius"]) {
[self updateBorderRadius:[proxy valueForUndefinedKey:@"borderRadius"]];
}
[self updateViewShadowPath];
}

Expand Down Expand Up @@ -644,19 +647,82 @@ - (void)setBackgroundTopCap_:(id)value
}
}

- (void)setBorderRadius_:(id)radius
- (CGFloat)radiusFromObject:(id)object
{
TiDimension theDim = TiDimensionFromObject(radius);
if (TiDimensionIsDip(theDim)) {
self.layer.cornerRadius = MAX(theDim.value, 0);
TiDimension theDim = TiDimensionFromObject(object);
return TiDimensionIsDip(theDim) ? MAX(theDim.value, 0) : 0;
}

- (void)addCornerRadius:(NSArray *)radiusArray toLayer:(CALayer *)viewLayer
{
CGFloat topLeftRadius;
CGFloat bottomLeftRadius;
CGFloat topRightRadius;
CGFloat bottomRightRadius;

if (radiusArray.count >= 4) {
topLeftRadius = [self radiusFromObject:radiusArray[0]];
topRightRadius = [self radiusFromObject:radiusArray[1]];
bottomRightRadius = [self radiusFromObject:radiusArray[2]];
bottomLeftRadius = [self radiusFromObject:radiusArray[3]];
} else if (radiusArray.count >= 2) {
CGFloat radius = [self radiusFromObject:radiusArray[0]];
topLeftRadius = radius;
bottomRightRadius = radius;

radius = [self radiusFromObject:radiusArray[1]];
bottomLeftRadius = radius;
topRightRadius = radius;
} else if (radiusArray.count == 1) {
// For same corner radius, no need to create bezier path. Use CALayer's cornerRadius.
viewLayer.cornerRadius = [self radiusFromObject:radiusArray[0]];
return;
}

CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] initWithLayer:viewLayer];
CGRect rect = viewLayer.bounds;
UIBezierPath *bezierPath = [UIBezierPath bezierPath];
[bezierPath moveToPoint:CGPointMake(0 + topLeftRadius, 0)];
[bezierPath addLineToPoint:CGPointMake(rect.size.width - topRightRadius, 0)];
[bezierPath addQuadCurveToPoint:CGPointMake(rect.size.width, topRightRadius) controlPoint:CGPointMake(rect.size.width, 0)];
[bezierPath addLineToPoint:CGPointMake(rect.size.width, rect.size.height - bottomRightRadius)];
[bezierPath addQuadCurveToPoint:CGPointMake(rect.size.width - bottomRightRadius, rect.size.height) controlPoint:CGPointMake(rect.size.width, rect.size.height)];
[bezierPath addLineToPoint:CGPointMake(bottomLeftRadius, rect.size.height)];
[bezierPath addQuadCurveToPoint:CGPointMake(0, rect.size.height - bottomLeftRadius) controlPoint:CGPointMake(0, rect.size.height)];
[bezierPath addLineToPoint:CGPointMake(0, topLeftRadius)];
[bezierPath addQuadCurveToPoint:CGPointMake(0 + topLeftRadius, 0) controlPoint:CGPointMake(0, 0)];
[bezierPath closePath];

shapeLayer.path = bezierPath.CGPath;
shapeLayer.frame = viewLayer.bounds;
viewLayer.mask = shapeLayer;
}

- (void)updateBorderRadius:(id)radius
{
NSArray *cornerRadiusArray;
if ([radius isKindOfClass:[NSString class]]) {
cornerRadiusArray = [(NSString *)radius componentsSeparatedByString:@" "];
} else if ([radius isKindOfClass:[NSArray class]]) {
cornerRadiusArray = radius;
} else if ([radius isKindOfClass:[NSNumber class]]) {
cornerRadiusArray = [NSArray arrayWithObject:radius];
} else {
self.layer.cornerRadius = 0;
NSLog(@"[WARN] Invalid value specified for borderRadius.");
return;
}

if (cornerRadiusArray.count == 0) {
NSLog(@"[WARN] No value specified for borderRadius.");
return;
}

[self addCornerRadius:cornerRadiusArray toLayer:self.layer];
if (bgdImageLayer != nil) {
bgdImageLayer.cornerRadius = self.layer.cornerRadius;
[self addCornerRadius:cornerRadiusArray toLayer:bgdImageLayer];
}
if (gradientLayer != nil) {
gradientLayer.cornerRadius = self.layer.cornerRadius;
[self addCornerRadius:cornerRadiusArray toLayer:gradientLayer];
}
[self updateClipping];
}
Expand Down

0 comments on commit 34b3a93

Please sign in to comment.