Skip to content

Commit

Permalink
Fixed a layout bug when the layer padding is changed. Fixed issue #258.
Browse files Browse the repository at this point in the history
  • Loading branch information
eskroch committed Jul 10, 2011
1 parent c064048 commit 2ac1efb
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 36 deletions.
9 changes: 0 additions & 9 deletions framework/Source/CPTAxisSet.m
Expand Up @@ -72,15 +72,6 @@ +(CGFloat)defaultZPosition
return CPTDefaultZPositionAxisSet;
}

-(void)layoutSublayers
{
[super layoutSublayers];

NSArray *theAxes = self.axes;
[theAxes makeObjectsPerformSelector:@selector(setNeedsLayout)];
[theAxes makeObjectsPerformSelector:@selector(setNeedsDisplay)];
}

#pragma mark -
#pragma mark Accessors

Expand Down
25 changes: 22 additions & 3 deletions framework/Source/CPTBorderedLayer.m
Expand Up @@ -71,11 +71,12 @@ -(void)renderAsVectorInContext:(CGContextRef)context
[self.fill fillPathInContext:context];
self.masksToBounds = useMask;

if ( self.borderLineStyle ) {
CGFloat inset = self.borderLineStyle.lineWidth / 2.0;
CPTLineStyle *theLineStyle = self.borderLineStyle;
if ( theLineStyle ) {
CGFloat inset = theLineStyle.lineWidth / 2.0;
CGRect selfBounds = CGRectInset(self.bounds, inset, inset);

[self.borderLineStyle setLineStyleInContext:context];
[theLineStyle setLineStyleInContext:context];

if ( self.cornerRadius > 0.0 ) {
CGFloat radius = MIN(MIN(self.cornerRadius, selfBounds.size.width / 2.0), selfBounds.size.height / 2.0);
Expand All @@ -89,6 +90,24 @@ -(void)renderAsVectorInContext:(CGContextRef)context
}
}

#pragma mark -
#pragma mark Layout

-(void)sublayerMarginLeft:(CGFloat *)left top:(CGFloat *)top right:(CGFloat *)right bottom:(CGFloat *)bottom
{
[super sublayerMarginLeft:left top:top right:right bottom:bottom];

CPTLineStyle *theLineStyle = self.borderLineStyle;
if ( theLineStyle ) {
CGFloat inset = theLineStyle.lineWidth / 2.0;

*left += inset;
*top += inset;
*right += inset;
*bottom += inset;
}
}

#pragma mark -
#pragma mark Masking

Expand Down
1 change: 1 addition & 0 deletions framework/Source/CPTLayer.h
Expand Up @@ -78,6 +78,7 @@
/// @{
+(CGFloat)defaultZPosition;
-(void)pixelAlign;
-(void)sublayerMarginLeft:(CGFloat *)left top:(CGFloat *)top right:(CGFloat *)right bottom:(CGFloat *)bottom;
/// @}

@end
24 changes: 20 additions & 4 deletions framework/Source/CPTLayer.m
Expand Up @@ -460,20 +460,22 @@ -(void)layoutSublayers
// This is where we do our custom replacement for the Mac-only layout manager and autoresizing mask
// Subclasses should override to lay out their own sublayers
// Sublayers fill the super layer's bounds minus any padding by default
CGFloat leftPadding = self.paddingLeft;
CGFloat bottomPadding = self.paddingBottom;
CGFloat leftPadding, topPadding, rightPadding, bottomPadding;
[self sublayerMarginLeft:&leftPadding top:&topPadding right:&rightPadding bottom:&bottomPadding];

CGRect selfBounds = self.bounds;
CGSize subLayerSize = selfBounds.size;
subLayerSize.width -= leftPadding + self.paddingRight;
subLayerSize.width -= leftPadding + rightPadding;
subLayerSize.width = MAX(subLayerSize.width, 0.0);
subLayerSize.height -= self.paddingTop + bottomPadding;
subLayerSize.height -= topPadding + bottomPadding;
subLayerSize.height = MAX(subLayerSize.height, 0.0);

NSSet *excludedSublayers = [self sublayersExcludedFromAutomaticLayout];
for (CALayer *subLayer in self.sublayers) {
if (![excludedSublayers containsObject:subLayer] && [subLayer isKindOfClass:[CPTLayer class]]) {
subLayer.frame = CGRectMake(leftPadding, bottomPadding, subLayerSize.width, subLayerSize.height);
[subLayer setNeedsLayout];
[subLayer setNeedsDisplay];
}
}
}
Expand All @@ -483,6 +485,20 @@ -(NSSet *)sublayersExcludedFromAutomaticLayout
return [NSSet set];
}

/** @brief Returns the margins that should be left between the bounds of the receiver and all sublayers.
* @param left The left margin.
* @param top The top margin.
* @param right The right margin.
* @param bottom The bottom margin.
**/
-(void)sublayerMarginLeft:(CGFloat *)left top:(CGFloat *)top right:(CGFloat *)right bottom:(CGFloat *)bottom
{
*left = self.paddingLeft;
*top = self.paddingTop;
*right = self.paddingRight;
*bottom = self.paddingBottom;
}

#pragma mark -
#pragma mark Masking

Expand Down
20 changes: 0 additions & 20 deletions framework/Source/CPTPlotAreaFrame.m
Expand Up @@ -75,26 +75,6 @@ +(CGFloat)defaultZPosition
return CPTDefaultZPositionPlotAreaFrame;
}

-(void)layoutSublayers
{
CPTPlotArea *thePlotArea = self.plotArea;
if ( thePlotArea ) {
CGFloat leftPadding = self.paddingLeft;
CGFloat bottomPadding = self.paddingBottom;

CGRect selfBounds = self.bounds;
CGSize subLayerSize = selfBounds.size;
CGFloat lineWidth = self.borderLineStyle.lineWidth;

subLayerSize.width -= leftPadding + self.paddingRight + lineWidth;
subLayerSize.width = MAX(subLayerSize.width, 0.0);
subLayerSize.height -= self.paddingTop + bottomPadding + lineWidth;
subLayerSize.height = MAX(subLayerSize.height, 0.0);

thePlotArea.frame = CGRectMake(leftPadding, bottomPadding, subLayerSize.width, subLayerSize.height);
}
}

#pragma mark -
#pragma mark Accessors

Expand Down

0 comments on commit 2ac1efb

Please sign in to comment.