Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

[ios]Add a maximumWidthRatio property to customize maximum width of scale bar. #65

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions platform/ios/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Mapbox welcomes participation and contributions from everyone. Please read [CONT
## master

* Fix ornaments' view constraints bug when devices change to bold-text mode. ([#57](https://github.com/mapbox/mapbox-gl-native-ios/pull/57))
* Add a `maximumWidthRatio` property for customizing maximum width of scale bar. ([#65](https://github.com/mapbox/mapbox-gl-native-ios/pull/65))
m-stephen marked this conversation as resolved.
Show resolved Hide resolved

## 5.5.0 - November 5, 2019

Expand Down
7 changes: 7 additions & 0 deletions platform/ios/src/MGLMapView.h
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,13 @@ MGL_EXPORT
*/
@property (nonatomic, assign) CGPoint scaleBarMargins;

/**
The maximum width ratio of the scale bar relative to the map view's frame.
m-stephen marked this conversation as resolved.
Show resolved Hide resolved
This value is limited from 0.1 to 1.
Default is 0.5(half of map view's width).
*/
@property (nonatomic, assign) CGFloat scaleBarMaximumWidthRatio;
m-stephen marked this conversation as resolved.
Show resolved Hide resolved

/**
A control indicating the map’s direction and allowing the user to manipulate
the direction, positioned in the upper-right corner.
Expand Down
12 changes: 12 additions & 0 deletions platform/ios/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@ - (void)commonInit
_scaleBarConstraints = [NSMutableArray array];
_scaleBarPosition = MGLOrnamentPositionTopLeft;
_scaleBarMargins = MGLOrnamentDefaultPositionOffset;
_scaleBar.margins = MGLOrnamentDefaultPositionOffset;

[self installConstraints];

Expand Down Expand Up @@ -800,9 +801,20 @@ - (void)setScaleBarPosition:(MGLOrnamentPosition)scaleBarPosition {
- (void)setScaleBarMargins:(CGPoint)scaleBarMargins {
MGLLogDebug(@"Setting scaleBarMargins: (x:%f, y:%f)", scaleBarMargins.x, scaleBarMargins.y);
_scaleBarMargins = scaleBarMargins;
if([self.scaleBar isKindOfClass:[MGLScaleBar class]]) {
((MGLScaleBar *)self.scaleBar).margins = scaleBarMargins;
}
[self installScaleBarConstraints];
}

- (void)setScaleBarMaximumWidthRatio:(CGFloat)scaleBarMaximumWidthRatio {
m-stephen marked this conversation as resolved.
Show resolved Hide resolved
m-stephen marked this conversation as resolved.
Show resolved Hide resolved
_scaleBarMaximumWidthRatio = scaleBarMaximumWidthRatio;
if([self.scaleBar isKindOfClass:[MGLScaleBar class]]) {
((MGLScaleBar *)self.scaleBar).maximumWidthRatio = scaleBarMaximumWidthRatio;
}
[self updateScaleBar];
}

- (void)setCompassViewPosition:(MGLOrnamentPosition)compassViewPosition {
MGLLogDebug(@"Setting compassViewPosition: %lu", compassViewPosition);
_compassViewPosition = compassViewPosition;
Expand Down
9 changes: 9 additions & 0 deletions platform/ios/src/MGLScaleBar.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,13 @@
// Sets the scale and redraws the scale bar
@property (nonatomic, assign) CLLocationDistance metersPerPoint;

/*
The maximum width ratio of the scale bar relative to the map view's frame.
This value is limited from 0.1 to 1.
Default is 0.5(half of map view's width).
*/
@property (nonatomic, assign) CGFloat maximumWidthRatio;

@property (nonatomic, assign) CGPoint margins;

@end
21 changes: 20 additions & 1 deletion platform/ios/src/MGLScaleBar.mm
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ - (instancetype)initWithFrame:(CGRect)frame {

- (void)commonInit {
_size = CGSizeZero;
_maximumWidthRatio = 0.5;

_primaryColor = [UIColor colorWithRed:18.0/255.0 green:45.0/255.0 blue:17.0/255.0 alpha:1];
_secondaryColor = [UIColor colorWithRed:247.0/255.0 green:247.0/255.0 blue:247.0/255.0 alpha:1];
Expand Down Expand Up @@ -229,7 +230,25 @@ - (CGFloat)actualWidth {
- (CGFloat)maximumWidth {
// TODO: Consider taking Scale Bar margins into account here.
CGFloat fullWidth = CGRectGetWidth(self.superview.bounds);
return floorf(fullWidth / 2);

if(self.maximumWidthRatio < 0.1) {
self.maximumWidthRatio = 0.1;
}
else if (self.maximumWidthRatio > 1) {
self.maximumWidthRatio = 1;
}

CGFloat result = floorf(fullWidth * self.maximumWidthRatio);
return result > fullWidth - (self.margins.x * 2) ? fullWidth - (self.margins.x * 2) : result;
}

- (void)setMaximumWidthRatio:(CGFloat)maximumWidthRatio {
_maximumWidthRatio = maximumWidthRatio;

[self updateVisibility];

self.recalculateSize = YES;
[self invalidateIntrinsicContentSize];
}

- (CGFloat)unitsPerPoint {
Expand Down