From 56b004e0208bee9df35f7287c2881b1e7a83ec3c Mon Sep 17 00:00:00 2001 From: Rene van den Berg Date: Fri, 13 Nov 2015 13:31:28 +0100 Subject: [PATCH 1/2] Add @IBInspectable property for value/unit text offset --- Pod/Classes/MBCircularProgressBarLayer.h | 2 ++ Pod/Classes/MBCircularProgressBarLayer.m | 11 +++++++---- Pod/Classes/MBCircularProgressBarView.h | 3 +++ Pod/Classes/MBCircularProgressBarView.m | 9 +++++++++ 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/Pod/Classes/MBCircularProgressBarLayer.h b/Pod/Classes/MBCircularProgressBarLayer.h index b6f75df..568aa38 100644 --- a/Pod/Classes/MBCircularProgressBarLayer.h +++ b/Pod/Classes/MBCircularProgressBarLayer.h @@ -37,4 +37,6 @@ @property (nonatomic,assign) BOOL showUnitString; @property (nonatomic,assign) BOOL showValueString; +@property (nonatomic,assign) CGPoint textOffset; + @end diff --git a/Pod/Classes/MBCircularProgressBarLayer.m b/Pod/Classes/MBCircularProgressBarLayer.m index 13874b3..db01d60 100644 --- a/Pod/Classes/MBCircularProgressBarLayer.m +++ b/Pod/Classes/MBCircularProgressBarLayer.m @@ -33,7 +33,7 @@ @implementation MBCircularProgressBarLayer @dynamic valueFontName; @dynamic showUnitString; @dynamic showValueString; - +@dynamic textOffset; #pragma mark - Drawing @@ -131,6 +131,7 @@ - (void)drawText:(CGSize)rectSize context:(CGContextRef)c NSMutableAttributedString *text = [NSMutableAttributedString new]; NSString *formatString = [NSString stringWithFormat:@"%%.%df", (int)self.decimalPlaces]; + NSAttributedString* value = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:formatString, self.value] attributes:valueFontAttributes]; @@ -154,10 +155,12 @@ - (void)drawText:(CGSize)rectSize context:(CGContextRef)c } CGSize percentSize = [text size]; + CGPoint textCenter = CGPointMake( + rectSize.width/2-percentSize.width/2 + self.textOffset.x, + rectSize.height/2-percentSize.height/2 + self.textOffset.y + ); - [text drawAtPoint:CGPointMake(rectSize.width/2-percentSize.width/2, - rectSize.height/2-percentSize.height/2)]; - + [text drawAtPoint:textCenter]; } #pragma mark - Override methods to support animations diff --git a/Pod/Classes/MBCircularProgressBarView.h b/Pod/Classes/MBCircularProgressBarView.h index fd63087..e8cbce4 100644 --- a/Pod/Classes/MBCircularProgressBarView.h +++ b/Pod/Classes/MBCircularProgressBarView.h @@ -74,6 +74,9 @@ IB_DESIGNABLE /* The shape of the background bar cap {kCGLineCapButt=0, kCGLineCapRound=1, kCGLineCapSquare=2} */ @property (nonatomic,assign) IBInspectable NSInteger emptyCapType; +/* The offset of the value/unit text */ +@property (nonatomic,assign) IBInspectable CGPoint textOffset; + /* Set the value of the progress bar with animation */ -(void)setValue:(CGFloat)value animateWithDuration:(NSTimeInterval)duration; diff --git a/Pod/Classes/MBCircularProgressBarView.m b/Pod/Classes/MBCircularProgressBarView.m index cae8c48..6bd3201 100644 --- a/Pod/Classes/MBCircularProgressBarView.m +++ b/Pod/Classes/MBCircularProgressBarView.m @@ -60,6 +60,7 @@ -(void)initView:(CGRect)frame{ [self setShowUnitString:YES]; [self setShowValueString:YES]; [self setValueFontName:@"HelveticaNeue-Thin"]; + [self setTextOffset:CGPointMake(0, 0)]; [self setUnitFontName:@"HelveticaNeue-Thin"]; } @@ -253,6 +254,14 @@ -(BOOL)showUnitString{ return self.progressLayer.showUnitString; } +-(void)setTextOffset:(CGPoint)textOffset{ + self.progressLayer.textOffset = textOffset; +} + +-(CGPoint)textOffset{ + return self.progressLayer.textOffset; +} + #pragma mark - CALayer -(MBCircularProgressBarLayer*)progressLayer{ From fdfcbe9def179475eab72f015554aee5c2d5e037 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20van=20den=20Berg?= Date: Fri, 13 Nov 2015 14:53:34 +0100 Subject: [PATCH 2/2] Explain 'textOffset' property in the README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 99070d7..9583f1b 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,7 @@ progressCapType | NSInteger | The shape of the progress bar cap | {kCGLineCapBut emptyLineWidth | CGFloat | The width of the background bar (user space units) | [0,∞) emptyLineColor | CGFloat | The color of the background bar | emptyCapType | CGFloat | The shape of the background bar cap | {kCGLineCapButt=0, kCGLineCapRound=1, kCGLineCapSquare=2} +textOffset | CGPoint | The offset to apply to the unit / value text | (0,0) = center of the circle ## Animation