Skip to content

Commit

Permalink
Merge pull request #9 from ReneB/add-offset-to-text-label
Browse files Browse the repository at this point in the history
Add offset to text label
  • Loading branch information
MatiBot committed Dec 20, 2015
2 parents bad9143 + fdfcbe9 commit e3cbb08
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Pod/Classes/MBCircularProgressBarLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@
@property (nonatomic,assign) BOOL showUnitString;
@property (nonatomic,assign) BOOL showValueString;

@property (nonatomic,assign) CGPoint textOffset;

@end
11 changes: 7 additions & 4 deletions Pod/Classes/MBCircularProgressBarLayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ @implementation MBCircularProgressBarLayer
@dynamic valueFontName;
@dynamic showUnitString;
@dynamic showValueString;

@dynamic textOffset;

#pragma mark - Drawing

Expand Down Expand Up @@ -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];

Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions Pod/Classes/MBCircularProgressBarView.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
9 changes: 9 additions & 0 deletions Pod/Classes/MBCircularProgressBarView.m
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
}

Expand Down Expand Up @@ -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{
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit e3cbb08

Please sign in to comment.