Skip to content

Commit

Permalink
Support decimal format
Browse files Browse the repository at this point in the history
  • Loading branch information
lminhtm committed Jun 23, 2019
1 parent f2e7364 commit aa8f9b0
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 16 deletions.
2 changes: 1 addition & 1 deletion LMGaugeView.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "LMGaugeView"
s.version = "1.0.4"
s.version = "1.0.5"
s.summary = "LMGaugeView is a simple and customizable gauge control for iOS."
s.homepage = "https://github.com/lminhtm/LMGaugeView"
s.license = 'MIT'
Expand Down
10 changes: 10 additions & 0 deletions LMGaugeView/LMGaugeView.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ IB_DESIGNABLE
*/
@property (nonatomic, assign) IBInspectable CGFloat ringThickness;

/*!
* A boolean indicates whether to show ring background.
*/
@property (nonatomic, assign) IBInspectable BOOL showRingBackground;

/*!
* The background color of the ring.
*/
Expand Down Expand Up @@ -138,6 +143,11 @@ IB_DESIGNABLE
*/
@property (nonatomic, strong) IBInspectable UIColor *unitOfMeasurementTextColor;

/*!
* A boolean indicates whether to show decimal value.
*/
@property (nonatomic, assign) IBInspectable BOOL decimalFormat;

/*!
* The receiver of all gauge view delegate callbacks.
*/
Expand Down
37 changes: 24 additions & 13 deletions LMGaugeView/LMGaugeView.m
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ - (void)initialize
_numOfSubDivisions = kDefaultNumOfSubDivisions;

// Ring
_showRingBackground = YES;
_ringThickness = kDefaultRingThickness;
_ringBackgroundColor = kDefaultRingBackgroundColor;

Expand Down Expand Up @@ -165,11 +166,13 @@ - (void)drawRect:(CGRect)rect
/*!
* Draw the ring background
*/
CGContextSetLineWidth(context, self.ringThickness);
CGContextBeginPath(context);
CGContextAddArc(context, center.x, center.y, ringRadius, 0, M_PI * 2, 0);
CGContextSetStrokeColorWithColor(context, [self.ringBackgroundColor colorWithAlphaComponent:0.3].CGColor);
CGContextStrokePath(context);
if (self.showRingBackground) {
CGContextSetLineWidth(context, self.ringThickness);
CGContextBeginPath(context);
CGContextAddArc(context, center.x, center.y, ringRadius, 0, M_PI * 2, 0);
CGContextSetStrokeColorWithColor(context, [self.ringBackgroundColor colorWithAlphaComponent:0.3].CGColor);
CGContextStrokePath(context);
}

/*!
* Draw the ring progress background
Expand Down Expand Up @@ -264,10 +267,15 @@ - (void)drawRect:(CGRect)rect
self.valueLabel = [[UILabel alloc] init];
self.valueLabel.backgroundColor = [UIColor clearColor];
self.valueLabel.textAlignment = NSTextAlignmentCenter;
self.valueLabel.text = [NSString stringWithFormat:@"%0.f", self.value];
if (self.decimalFormat) {
self.valueLabel.text = [NSString stringWithFormat:@"%.1f", self.value];
}
else {
self.valueLabel.text = [NSString stringWithFormat:@"%0.f", self.value];
}
self.valueLabel.font = self.valueFont;
self.valueLabel.adjustsFontSizeToFitWidth = YES;
self.valueLabel.minimumScaleFactor = 10/self.valueLabel.font.pointSize;
self.valueLabel.minimumScaleFactor = 0.5;
self.valueLabel.textColor = self.valueTextColor;
[self addSubview:self.valueLabel];
}
Expand All @@ -288,7 +296,7 @@ - (void)drawRect:(CGRect)rect
}
self.minValueLabel.text = [NSString stringWithFormat:@"%0.f", self.minValue];
self.minValueLabel.font = self.minMaxValueFont;
self.minValueLabel.minimumScaleFactor = 10/self.minValueLabel.font.pointSize;
self.minValueLabel.minimumScaleFactor = 0.5;
self.minValueLabel.textColor = self.minMaxValueTextColor;
self.minValueLabel.hidden = !self.showMinMaxValue;
CGPoint minDotCenter = CGPointMake(dotRadius * cos(self.startAngle) + center.x, dotRadius * sin(self.startAngle) + center.y);
Expand All @@ -307,7 +315,7 @@ - (void)drawRect:(CGRect)rect
}
self.maxValueLabel.text = [NSString stringWithFormat:@"%0.f", self.maxValue];
self.maxValueLabel.font = self.minMaxValueFont;
self.maxValueLabel.minimumScaleFactor = 10/self.maxValueLabel.font.pointSize;
self.maxValueLabel.minimumScaleFactor = 0.5;
self.maxValueLabel.textColor = self.minMaxValueTextColor;
self.maxValueLabel.hidden = !self.showMinMaxValue;
CGPoint maxDotCenter = CGPointMake(dotRadius * cos(self.endAngle) + center.x, dotRadius * sin(self.endAngle) + center.y);
Expand All @@ -324,7 +332,7 @@ - (void)drawRect:(CGRect)rect
self.unitOfMeasurementLabel.text = self.unitOfMeasurement;
self.unitOfMeasurementLabel.font = self.unitOfMeasurementFont;
self.unitOfMeasurementLabel.adjustsFontSizeToFitWidth = YES;
self.unitOfMeasurementLabel.minimumScaleFactor = 10/self.unitOfMeasurementLabel.font.pointSize;
self.unitOfMeasurementLabel.minimumScaleFactor = 0.5;
self.unitOfMeasurementLabel.textColor = self.unitOfMeasurementTextColor;
[self addSubview:self.unitOfMeasurementLabel];
self.unitOfMeasurementLabel.hidden = !self.showUnitOfMeasurement;
Expand Down Expand Up @@ -367,7 +375,12 @@ - (void)setValue:(CGFloat)value
/*!
* Set text for value label
*/
self.valueLabel.text = [NSString stringWithFormat:@"%0.f", _value];
if (self.decimalFormat) {
self.valueLabel.text = [NSString stringWithFormat:@"%.1f", _value];
}
else {
self.valueLabel.text = [NSString stringWithFormat:@"%0.f", _value];
}

/*!
* Trigger the stoke animation of ring layer.
Expand Down Expand Up @@ -516,7 +529,6 @@ - (void)setValueFont:(UIFont *)valueFont
_valueFont = valueFont;

self.valueLabel.font = _valueFont;
self.valueLabel.minimumScaleFactor = 10/_valueFont.pointSize;
}
}

Expand Down Expand Up @@ -580,7 +592,6 @@ - (void)setUnitOfMeasurementFont:(UIFont *)unitOfMeasurementFont
_unitOfMeasurementFont = unitOfMeasurementFont;

self.unitOfMeasurementLabel.font = _unitOfMeasurementFont;
self.unitOfMeasurementLabel.minimumScaleFactor = 10/_unitOfMeasurementFont.pointSize;
}
}

Expand Down
Binary file not shown.
4 changes: 2 additions & 2 deletions LMGaugeViewDemo/LMGaugeViewDemo/LMGaugeViewDemo-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0.4</string>
<string>1.0.5</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>40</string>
<string>50</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UILaunchStoryboardName</key>
Expand Down

0 comments on commit aa8f9b0

Please sign in to comment.