summaryrefslogtreecommitdiff
path: root/platform/ios/src/MGLScaleBar.mm
diff options
context:
space:
mode:
Diffstat (limited to 'platform/ios/src/MGLScaleBar.mm')
-rw-r--r--platform/ios/src/MGLScaleBar.mm29
1 files changed, 14 insertions, 15 deletions
diff --git a/platform/ios/src/MGLScaleBar.mm b/platform/ios/src/MGLScaleBar.mm
index f17d7b7ad2..9590a99438 100644
--- a/platform/ios/src/MGLScaleBar.mm
+++ b/platform/ios/src/MGLScaleBar.mm
@@ -86,7 +86,7 @@ static const MGLRow MGLImperialTable[] ={
@property (nonatomic, assign) CGFloat borderWidth;
@property (nonatomic) NSCache* labelImageCache;
@property (nonatomic) MGLScaleBarLabel* prototypeLabel;
-
+@property (nonatomic) CGFloat lastLabelWidth;
@end
@@ -94,6 +94,7 @@ static const CGFloat MGLBarHeight = 4;
static const CGFloat MGLFeetPerMeter = 3.28084;
@interface MGLScaleBarLabel : UILabel
+
@end
@implementation MGLScaleBarLabel
@@ -184,7 +185,7 @@ static const CGFloat MGLFeetPerMeter = 3.28084;
#pragma mark - Dimensions
- (CGSize)intrinsicContentSize {
- return CGSizeMake(self.actualWidth, 16);
+ return self.actualWidth > 0 ? CGSizeMake(ceil(self.actualWidth + self.lastLabelWidth/2), 16) : CGSizeZero;
}
- (CGFloat)actualWidth {
@@ -194,8 +195,7 @@ static const CGFloat MGLFeetPerMeter = 3.28084;
- (CGFloat)maximumWidth {
CGFloat fullWidth = CGRectGetWidth(self.superview.bounds);
- CGFloat padding = [self usesRightToLeftLayout] ? fullWidth - CGRectGetMaxX(self.frame) : CGRectGetMinX(self.frame);
- return floorf(fullWidth / 2 - padding);
+ return floorf(fullWidth / 2);
}
- (CGFloat)unitsPerPoint {
@@ -256,12 +256,9 @@ static const CGFloat MGLFeetPerMeter = 3.28084;
[self updateVisibility];
self.row = [self preferredRow];
-
- CGSize size = self.intrinsicContentSize;
- self.frame = CGRectMake(CGRectGetMinX(self.frame),
- CGRectGetMinY(self.frame),
- size.width,
- size.height);
+
+ [self invalidateIntrinsicContentSize];
+ [self setNeedsLayout];
}
- (void)updateVisibility {
@@ -371,7 +368,9 @@ static const CGFloat MGLFeetPerMeter = 3.28084;
CLLocationDistance barDistance = multiplier * i;
UIImage *image = [self cachedLabelImageForDistance:barDistance];
-
+ if (i == self.row.numberOfBars) {
+ self.lastLabelWidth = image.size.width;
+ }
labelView.layer.contents = (id)image.CGImage;
labelView.layer.contentsScale = image.scale;
}
@@ -398,7 +397,7 @@ static const CGFloat MGLFeetPerMeter = 3.28084;
}
- (void)layoutBars {
- CGFloat barWidth = round((CGRectGetWidth(self.bounds) - self.borderWidth * 2.0f) / self.bars.count);
+ CGFloat barWidth = round((self.intrinsicContentSize.width - self.borderWidth * 2.0f) / self.bars.count);
NSUInteger i = 0;
for (UIView *bar in self.bars) {
@@ -409,7 +408,7 @@ static const CGFloat MGLFeetPerMeter = 3.28084;
}
self.containerView.frame = CGRectMake(CGRectGetMinX(self.bars.firstObject.frame),
- CGRectGetMaxY(self.bounds)-MGLBarHeight,
+ self.intrinsicContentSize.height-MGLBarHeight,
self.actualWidth,
MGLBarHeight+self.borderWidth*2);
@@ -421,12 +420,12 @@ static const CGFloat MGLFeetPerMeter = 3.28084;
}
- (void)layoutLabels {
- CGFloat barWidth = round(self.bounds.size.width / self.bars.count);
+ CGFloat barWidth = round(self.actualWidth / self.bars.count);
BOOL RTL = [self usesRightToLeftLayout];
NSUInteger i = RTL ? self.bars.count : 0;
for (UIView *label in self.labelViews) {
CGFloat xPosition = round(barWidth * i - CGRectGetMidX(label.bounds) + self.borderWidth);
- CGFloat yPosition = round(0.5 * (CGRectGetMaxY(self.bounds) - MGLBarHeight));
+ CGFloat yPosition = round(0.5 * (self.intrinsicContentSize.height - MGLBarHeight));
CGRect frame = label.frame;
frame.origin.x = xPosition;