Skip to content

Commit

Permalink
Unit string shown under numerical value
Browse files Browse the repository at this point in the history
  • Loading branch information
chuckboris committed Feb 18, 2019
1 parent 8893926 commit ba63be7
Showing 1 changed file with 49 additions and 46 deletions.
95 changes: 49 additions & 46 deletions Pod/Classes/MBCircularProgressBarLayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ - (void) drawInContext:(CGContextRef) context{
[self drawProgressBar:rect context:context];

if (self.showValueString){
[self drawText:rect context:context];
[self drawText:rect.size context:context];
}

UIGraphicsPopContext();
Expand Down Expand Up @@ -143,52 +143,55 @@ - (void)drawProgressBar:(CGRect)rect context:(CGContextRef)c{
CGPathRelease(strokedArc);
}

- (void)drawText:(CGRect)rect context:(CGContextRef)c{
NSMutableParagraphStyle* textStyle = NSMutableParagraphStyle.defaultParagraphStyle.mutableCopy;
textStyle.alignment = NSTextAlignmentLeft;

CGFloat valueFontSize = self.valueFontSize == -1 ? CGRectGetHeight(rect)/5 : self.valueFontSize;

NSDictionary* valueFontAttributes = @{NSFontAttributeName: [UIFont fontWithName: self.valueFontName size:valueFontSize], NSForegroundColorAttributeName: self.fontColor, NSParagraphStyleAttributeName: textStyle};

NSMutableAttributedString *text = [NSMutableAttributedString new];

NSString *formatString = [NSString stringWithFormat:@"%%.%df", (int)self.decimalPlaces];

NSString* textToPresent;
if (self.countdown) {
textToPresent = [NSString stringWithFormat:formatString, (self.maxValue - self.value)];
} else {
textToPresent = [NSString stringWithFormat:formatString, self.value];
}
NSAttributedString* value = [[NSAttributedString alloc] initWithString:textToPresent
- (void)drawText:(CGSize)rectSize context:(CGContextRef)c
{
NSMutableParagraphStyle* textStyle = NSMutableParagraphStyle.new;
textStyle.alignment = NSTextAlignmentCenter;

CGFloat valueFontSize = self.valueFontSize == -1 ? rectSize.height/5 : self.valueFontSize;

NSDictionary* valueFontAttributes = @{NSFontAttributeName: [UIFont fontWithName: self.valueFontName size:valueFontSize], NSForegroundColorAttributeName: self.fontColor, NSParagraphStyleAttributeName: textStyle};

NSMutableAttributedString *text = [NSMutableAttributedString new];

NSString *formatString = [NSString stringWithFormat:@"%%.%df", (int)self.decimalPlaces];

NSString* textToPresent;
if (self.countdown) {
textToPresent = [NSString stringWithFormat:formatString, (self.maxValue - self.value)];
} else {
textToPresent = [NSString stringWithFormat:formatString, self.value];
}
NSAttributedString* value = [[NSAttributedString alloc] initWithString:textToPresent
attributes:valueFontAttributes];
[text appendAttributedString:value];

// set the decimal font size
NSUInteger decimalLocation = [text.string rangeOfString:@"."].location;
if (decimalLocation != NSNotFound){
NSDictionary* valueDecimalFontAttributes = @{NSFontAttributeName: [UIFont fontWithName: self.valueFontName size:self.valueDecimalFontSize == -1 ? valueFontSize : self.valueDecimalFontSize], NSForegroundColorAttributeName: self.fontColor, NSParagraphStyleAttributeName: textStyle};
NSRange decimalRange = NSMakeRange(decimalLocation, text.length - decimalLocation);
[text setAttributes:valueDecimalFontAttributes range:decimalRange];
}

// ad the unit only if specified
if (self.showUnitString) {
NSDictionary* unitFontAttributes = @{NSFontAttributeName: [UIFont fontWithName: self.unitFontName size:self.unitFontSize == -1 ? CGRectGetHeight(rect)/7 : self.unitFontSize], NSForegroundColorAttributeName: self.fontColor, NSParagraphStyleAttributeName: textStyle};

NSAttributedString* unit =
[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", self.unitString] attributes:unitFontAttributes];
[text appendAttributedString:unit];
}

CGSize percentSize = [text size];
CGPoint textCenter = CGPointMake(
CGRectGetMidX(rect)-percentSize.width/2 + self.textOffset.x,
CGRectGetMidY(rect)-percentSize.height/2 + self.textOffset.y
);

[text drawAtPoint:textCenter];
[text appendAttributedString:value];

// set the decimal font size
NSUInteger decimalLocation = [text.string rangeOfString:@"."].location;
if (decimalLocation != NSNotFound){
NSDictionary* valueDecimalFontAttributes = @{NSFontAttributeName: [UIFont fontWithName: self.valueFontName size:self.valueDecimalFontSize == -1 ? valueFontSize : self.valueDecimalFontSize], NSForegroundColorAttributeName: self.fontColor, NSParagraphStyleAttributeName: textStyle};
NSRange decimalRange = NSMakeRange(decimalLocation, text.length - decimalLocation);
[text setAttributes:valueDecimalFontAttributes range:decimalRange];
}

// ad the unit only if specified
if (self.showUnitString) {
NSDictionary* unitFontAttributes = @{NSFontAttributeName: [UIFont fontWithName: self.unitFontName size:self.unitFontSize == -1 ? rectSize.height/7 : self.unitFontSize], NSForegroundColorAttributeName: self.fontColor, NSParagraphStyleAttributeName: textStyle};

NSAttributedString* unit =
[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"\n%@", self.unitString] attributes:unitFontAttributes];
[text appendAttributedString:unit];
}

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
);

CGRect rect = { textCenter, percentSize };

[text drawInRect:rect];
}

#pragma mark - Override methods to support animations
Expand Down

0 comments on commit ba63be7

Please sign in to comment.